Afnan Bashir
Afnan Bashir

Reputation: 7429

Is it Yii or is it me who is wrong?

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

Upvotes: 0

Views: 912

Answers (4)

Onkar Janwa
Onkar Janwa

Reputation: 3950

Yii CActiveRecord has a variable isNewRecord, its value is true when an object is created and remains true until a find or save function is not called. When a model calls save function, isNewRecord set to false.

So in your case you need to set this variable "isNewRecord" true each time before save function called or create a new model each time than call the save function.

See the changes in your code::

$model->setIsNewRecord(True); // changes code line

   protected function saveImage($formData,$model)
    {                   
           if ($formData === null)
               return;                   
            $idx=0;

        foreach($formData['title'] as $image)
        {
            $model->setIsNewRecord(True); // for each new record
            $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;
    }

Upvotes: 0

Afnan Bashir
Afnan Bashir

Reputation: 7429

And once again I am wrong :)

Issue was that in each foreach ietration the $model was previous and New instant of $model was not created and yii thought that hey its old one probably this Geek is updating it. so that is it.it updates in database.

just did $model = new HotelImages;

before populating them

I thank fivedigit for Just an idea.

I have a strong believe on this


Most people says Little knowledge is dangerous...well that is true for all except for programmers. Programmers are rare species and what it all takes is a little idea/hint to do the thing

Upvotes: 0

Dan Blows
Dan Blows

Reputation: 21184

@fivedigit's analysis is correct. My suggested fix would be to reduce the complexity of this method. Put everything inside the foreach in its own method (e.g. saveImage), then do something like:

foreach($formData['title'] as $image) {
  $this->saveImage($image, new Image);
}

This method is a bit of a super-method, and you might find it easier to break it out into different methods. Perhaps use a component to handle the file copying, instead of filling up the controller.

Upvotes: 0

fivedigit
fivedigit

Reputation: 18682

The save() method inserts a record into the database if it doesn't exist yet, and otherwise updates that same database record.

$model is being passed in as a method parameter, and I'm just assuming here that its title attribute 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.

Upvotes: 4

Related Questions