E
E
EVOSandru62015-01-27 13:15:19
Yii
EVOSandru6, 2015-01-27 13:15:19

How to update in Yii without foreach for all models, the result of a find..() selection?

Good afternoon,
I have an array of descendant models:

$category=Category::model()->findByPk(1);
$descendants=$category->descendants()->findAll();

How can I update for all $descendants field, for example EXIST to value "0" with one UpdateAll without foreach ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2015-01-27
@fornit1917

Don't write find at all then and don't work with the Category model. Work with the model you are going to update.
I can guess something like this:
If you want to update the array of selected ARs and do not want to write foreach to any, then you can use array_walk php.net/manual/en/function.array-walk.php

I
Ivan, 2015-01-30
@IvanCher

If I understand correctly, then try this:

$category=Category::model()->findByPk(1);
$descendants=$category->descendants()->findAll();
$ids = array_map(function($descendant) {
    return $descendant->id;
}, $descendants);

Category::model()->addInCondition('id', $ids)->updateAll(array('exist' => 0));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question