Reputation: 367
<pre>
foreach($cons_name as $name){
$model = new Apps();
$model->const = $name;
$model->value =$value[$i];
$model->save();
}
</pre>
i am using this code how can i add some "where" conditions in this scenario. i am new to yii please tell me how to solve this issue. i am trying to save multiple records with the give code it is working for me but i want to add some more conditions to it. also tell me how can i add multiple rows at a time instead of using loop
Upvotes: 0
Views: 781
Reputation: 1
Because you use array with Active Record objects, you need foreach it and set necessary data for each object and then save.
You can use method ActiveRecord.updateAll();
Upvotes: 0
Reputation: 6202
$model = new Apps();
will create a new object for your model. If you want to edit an existing row, you should fetch the corresponding row using find
method.
$model = Apps::model()->find('id=1 and type = 2');
As far as I know, you cannot insert multiple rows without using any loop in PHP. What you can do is build you insert query with multiple rows separated by comma and execute it once.
like
INSERT INTO table1 (id,name) VALUES (1,'NAME1'),(2,'NAME2'),(3,'NAME3')
Upvotes: 3