Not Updating The Image Doesn't Update The Record

Hi all,

the problem is if i want to update the other fields and not the image … it passes the validation and doesn’t update any of the fields .

But if i upadte the image and other fields it updates.

or

if i upadte the image it updates.

view

[code]

     <?php echo $form->labelEx($model,'pimg'); ?>


    <?php echo $form->fileField($model, 'pimg',array('id'=>'imgInput',)); ?>	


<?php echo $form->error($model,'pimg'); ?>

controller

        public function actionEdit($id)


    {


        $model=$this->loadModel($id);


    if(isset($_POST['Product']))


    {


    $model->pimg=CUploadedFile::getInstance($model,'pimg');	                                 $fileName=$model->pimg;


    $model->attributes=$_POST['Product'];


    if($model->save())


    $model->pimg->saveAs('images/'.$fileName);


    $this->redirect(array('display','id'=>$model->productid));


    }


       $this->render('edit',array('model'=>$model,));


	


    }





      model rules





        array('name, category, model, brand, description, price', 'required'),


    array('pimg', 'file','types'=>'jpg','on'=>'create', 'allowEmpty'=>false),


    array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),

[code]

I think the problem is with the contorller i keep getting the error

Fatal error: Call to a member function saveAs() on a non-object in D:\wamp\www\testfolder\protected\controllers\ProductController.php on line 147

line 147 $model->pimg->saveAs(‘images/’.$fileName);

image appears but then image name from db doesn’t appear next to the choose file button renders stating no file choosen

PLEASE HELP! :unsure: :-[ :mellow: I am new to Yii and am loosing my mind on this. Thank you.

This is what am done with … trust me it works :) yup

[code]

  $model=$this->loadModel($id);





if(isset($_POST['Product']))


{


        $model->attributes=$_POST['Product'];


$fileName=CUploadedFile::getInstance($model,'pimg');        


    if($model->pimg!= '' && $fileName==='')


{


 if($model->save())


    $this->redirect(array('display','id'=>$model->productid));


}


else


        {


 if (is_object($fileName) && get_class($fileName)==='CUploadedFile')


         $model->pimg = $fileName;





         if (is_object($model->pimg))


         $model->pimg->saveAs('images/'.$fileName);





if($model->save())





    $this->redirect(array('display','id'=>$model->productid));


    }


}





$this->render('edit',array('model'=>$model,));

[code]

i have the same error this error come on my view if i donot select any image to upload but if i select the image and submit then this error not comes i also image required in model but validation not working for image Any guidance???

just add a scenario on model rule functions…




array('image_name','required','on'=>'create'),


array('image_name', 'file', 'allowEmpty' => TRUE,'types' => 'jpg, jpeg, gif, png','on'=>'update'),

i used this rules but only this image rule is not working all other working fine.Any other solution please

can you please post a model code.

here is my full Mooel code


class Furniture extends CActiveRecord

{

	/**

	 * @return string the associated database table name

	 */

	 public $image;

    public $uploadimage;

    public $uploadedFile;

	public function tableName()

	{

		return 'furniture';

	}

public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('gender, country, city, age,username,image,password,cpassword', 'required'),

			 array('username', 'unique', 'attributeName'=> 'username', 'caseSensitive' => 'false'),

			  array('password', 'length', 'min' =>6),

			   array('city', 'length', 'min' =>4),

            array('password', 'length', 'max' =>32),

            array('cpassword', 'compare', 'allowEmpty' => 'false', 'compareAttribute' => 'password', 'strict' => 'true'),

			array('gender, country, city, age,username,password,cpassword', 'length', 'max'=>32),

		

		array('image', 'length', 'min'=>5, 'on'=>'create,insert,update'),

			array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>False, 'on'=>'update'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

				array('gender, country, city, age,username,password,cpassword, image', 'safe', 'on'=>'search'),

		);

	}

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'gender' => 'gender',

			'country' => 'country',

			'city' => 'city',

			'age' => 'age',

			'username'=>'username',

			'password'=>'password',

			'cpassword'=>'cpassword',

			'image'=>'image',

		);

	}


public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('gender',$this->gender,true);

		$criteria->compare('country',$this->country,true);

		$criteria->compare('city',$this->city);

		$criteria->compare('age',$this->age);

		$criteria->compare('password',$this->password);

		$criteria->compare('cpassword',$this->cpassword);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

}



Hi

why you can write a two times on=>‘update’ in model rule (insert and update) ?





array('image', 'length', 'min'=>5, 'on'=>'create,insert,update'), 

 array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>False, 'on'=>'update'),

change to


array('image', 'length', 'min'=>5, 'on'=>'create'), 

 array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>False, 'on'=>'update'),



and also remove the image on required field on line 1


array('gender, country, city, age,username,password,cpassword', 'required'),

I hope it’s solved

Thanks buddy i solve this problem… actullay for filefield validation not working first we should go in controller and add




 $model=new yourcontroller;  

$model=new yourcontroller('create'); // here create is your submit button



Then for file field validation work

Again Thanks

not working

:(click for the actual issue

PLEASE let me know where’s the problem

not working

:(click for the actual issue

PLEASE let me know where’s the problem