When a user looks at their messages I wish to update each message to READ. I do this by setting the read field to 1/true.
The saveAll does not seems to update the records after my find. I am not sure is the problem with my foreach loop or saveAll, I get no error.
array(
'Message' => array(
'id' => '1',
'message_text' => 'this isi a test messge',
'book_id' => '2',
'page_id' => '1',
'user_id' => '537765a1-d64c-4529-aa09-5af648257ec7',
'read' => false,
'modified' => '2014-05-26 22:36:32',
'created' => '2014-05-26 22:36:32'
),
'Book' => array(
'id' => '2',
'user_id' => '537765',
'title' => 'test book 2',
'modified' => '2014-05-18 14:47:30',
'created' => '2014-05-18 14:47:30'
),
)
or if there are many messages
array(
(int) 0 => array(
'Message' => array(
'id' => '1',
'message_text' => 'this isi a test messge',
'book_id' => '2',
'page_id' => '1',
'user_id' => '537765a1-d64c-4529-aa09-5af648257ec7',
'read' => false,
'modified' => '2014-05-26 22:36:32',
'created' => '2014-05-26 22:36:32'
),
'Book' => array(
'id' => '2',
'user_id' => '537765',
'title' => 'test book 2',
'modified' => '2014-05-18 14:47:30',
'created' => '2014-05-18 14:47:30'
),
),
(int) 1 => array(
'Message' => array(
'id' => '2',
'message_text' => 'hguhghu uhghuguhg',
'book_id' => '0',
'page_id' => '0',
'user_id' => '537765a1-d64c-4529-aa09-5af648257ec7',
'read' => false,
'modified' => '0000-00-00 00:00:00',
'created' => '0000-00-00 00:00:00'
),
'Book' => array(
'id' => null,
'user_id' => null,
'title' => null,
'modified' => null,
'created' => null
),
)
)
My controller code messagesController.php
public function index() {
$this->Message->recursive = 0;
$conditions = array(
'conditions' => array('Message.user_id' => $this->Auth->user('id')),
);
$messages = $this->Message->find('all');
foreach( $messages as $m ):
$m['Message']['read'] = true;
endforeach;
$this->Page->saveAll($messages['Message']);
$this->set('messages', $messages);
}
Another little query on this also is that would this be better suited in the model? Everytime a find is called the model will update the 'read' field.