CakePHP: how to delete haveMany associations with saveAll
I have a model model1
which hasMany model2
. How can I remove all the model2s
from a specific model1
, e.g. the one with ID 1234? This means, in SQL,
UPDATE
model2
SET
model1_id=NULL
WHERE
model1_id=1234;
But how to formulate this the CakePHP wa开发者_运维知识库y? I tried model1->saveAll
with the following array as argument:
Array
(
[Model1] => Array
(
[id] => 1234
)
[Model2] => Array
(
)
)
Which means, Model2 just set to a empty array. But this doesn't work. Now, how can I "un-associate" objects2 which belong to another specific object1 in a hasMany (seen from object1 side) relationship?
Try using updateAll($fields, $conditions)
$this->Model2->updateAll(array('model1_id' => null), array('model1_id' => 1234));
Here's the CakePHP Docs about this.
精彩评论