문제

I am using the following code to retrieve all messages that have the same thread_id for a customer portal project I am working on.

public function view_thread($thread_id = null) {
    if (!$this->Message->exists($thread_id)) {
        throw new NotFoundException(__('Invalid message'));
    }
    $options = array('conditions' => array('Message.thread_id' => $thread_id));
    $messages = $this->Message->find('all', $options);
    $this->set(compact('messages'));    
}

At the moment the $thread_id is set as an integer, although I would eventually like to use a random string.

It works OK if the $thread_id also happens to be a record id in the messages table e.g. 1 as seen in the table below, however, if the $thread_id value does not correspond with a record id I get an error -

'/messages/view_thread/5' was not found on this server

even though there are messages in the table with an $thread_id of 5

id    thread_id    subject
1     1            Test Message
2     1            Re: Test Message
6     1            Re: Test Message
12    1            Re: Test Message
24    5            New Test Message
25    5            Re: New Test Message

I can't for the life of me work out what is going on, can anyone help me.

도움이 되었습니까?

해결책

I think you missunderstood the $this->Model->exists(); It will look for id field not the $thread_id you provided.... So, try this-

public function view_thread($thread_id = null) {
    if(empty($thread_id)){
        throw new NotFoundException(__('Invalid message'));
    }

    $options = array('conditions' => array('Message.thread_id' => $thread_id));
    $messages = $this->Message->find('all', $options);

    if(empty($messages)){
        throw new NotFoundException(__('Invalid message'));
    }   
    $this->set(compact('messages'));    
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top