当用户查看他们的消息时,我希望将每条消息更新为 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
),
)
)
我的控制器代码messageController.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 时,模型都会更新“读取”字段。