0

I was reading through http://book.cakephp.org/3.0/en/orm/query-builder.html

when I found myself wondering if the usual callbacks like Model.afterSave will still triggered if I write a query like this:

$query = $articles->query();
$query->update()
->set(['published' => true])
->where(['id' => $id])
->execute();

What should I do if it does not and I still want to trigger the callbacks while using the query builder to run update operations?

The reason is because I want to update another model called Authors after this query is updated.

4

1 回答 1

1

使用查询触发回调的唯一方法是找到要更新的每条记录并手动保存:

$query = $articles->query()->where(...);
foreach ($query as $entity) { $entity->published = true; $articles->save($entity); }
于 2014-10-28T14:58:54.303 回答