Hi there I have written a code that ietrates through whole array and copies the images to a folder and inserts data in record. Now funny thing is that $model->save()does not show error it returns true. and program never goes into else
now what happens loop continues to run and completes its ietration without breaking. I can not guess who is wrong. greater chances are me as i am most of time 🙂
here is code
protected function saveImage($formData,$model)
{
if ($formData === null)
return;
$idx=0;
foreach($formData['title'] as $image)
{
$model->title = $image;
$file= dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR
. DIRECTORY_SEPARATOR .'images\hotelImages'. DIRECTORY_SEPARATOR
. $model->hotel->name;
$model->image = Yii::app()->baseUrl. "/images/hotelImages/".$_FILES['HotelImages']['name'][$idx];//image path
if($model->save())
{
echo $idx.'<br /> it was sucess<br />';
If(file_exists($file))
{
copy($_FILES['HotelImages']['tmp_name'][$idx],$file."/".$_FILES['HotelImages']['name'][$idx]);
}
else
{
mkdir($file);
copy($_FILES['HotelImages']['tmp_name'][$idx],$file."/".$_FILES['HotelImages']['name'][$idx]);
}
$idx++;
}//if there was error
else
{
print_r($model->getErrors());
yii::app()->end();
return FALSE;
}
echo '<br />end reached <br />';
}
yii::app()->end();
return true;
}
var_dump for $formdata is
array
(
'title' => array
(
'0' => 'title1'
'1' => 'title2'
)
)
No mater what ever the ietration count for foreach loop database gets only single row
The
save()method inserts a record into the database if it doesn’t exist yet, and otherwise updates that same database record.$modelis being passed in as a method parameter, and I’m just assuming here that itstitleattribute is not the primary key.In other words, you keep updating the same database record over and over, which is why you only see one record in the database.