Reputation: 368
I have this loop for save records with Active record:
foreach ($array as $value) {
$modelImagen->url = $value;
$modelImagen->inmueble_id_inmueble = '1';
$modelImagen->save();
}
And i have the problem saves only one record, i think that maybe override the data and saves the last one. Maybe i have a problem with the loop? All the tables id are autoincremental.
Thanks.
Upvotes: 1
Views: 831
Reputation: 2055
initiate the model in every loop and unset
after save
Here is your code should be
foreach ($array as $value) {
$modelImagen = new yourmodelName(); // initiate your model
$modelImagen->url = $value;
$modelImagen->inmueble_id_inmueble = '1';
$modelImagen->save();
unset($modelImagen); // do unset
}
Upvotes: 0
Reputation: 7627
You need to create additional records/rows, your current loop overwrites the same record/row.
You can use either of the following methods to save all url values in new rows
foreach ($array as $value) {
$modelImagen = new ModelImagen() // Your model class
$modelImagen->url = $value;
$modelImagen->inmueble_id_inmueble = '1';
$modelImagen->save();
}
or you can use insert
method, note that validation is not performed in insert
method, you will have call validate()
separately if you want validation to be performed
Upvotes: 2
Reputation: 720
I think you need to new AR in the loop. e.g. $modelImagen=new Model;
Upvotes: 1