当用户查看他们的消息时,我希望将每条消息更新为READ。我这样做是通过将读取字段设置为1 / true。
在我找到之后,saveAll似乎没有更新记录。我不确定我的foreach循环或saveAll的问题,我没有错误。
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'
),
)
或者如果有很多消息
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
),
)
)
我的控制器代码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);
}
对此的另一个小问题是,这会更适合模型吗?每次调用find时,模型都会更新“read”字段。
答案 0 :(得分:0)
foreach( $messages as $m ) {
$m['Message']['read'] = true;
$this->Page->saveAll($m);
}