Trying to get property of non-object (SOLVED)

Hi all, I have the ‘property of non-object’ error in the following controller:


  public function actionCreate()

    {

        $model = new Announcements();


        if ($model->load(Yii::$app->request->post())) 

		{                                                            

			$imageName = $model->announcement_title;

			$model->file = UploadedFile::getInstance($model,'file');

			

// Error Line >>	$model->announcement_image = $imageName.'.'.$model->file->extension;

			$model->save();	

I use the same upload process that works for products and I’m very challenged as to figuring out where I am not consistent in the process. If you need for me to provide table structure data or any other information, I will be glad to do so.

Thanks

my guess is that there isn’t a file being uploaded.




public function actionCreate()

{

  $model = new Announcements();

  if ($model->load(Yii::$app->request->post())) {                                  	

   $model->file = UploadedFile::getInstance($model,'file');

   if($model->file){

    $model->announcement_image = $model->announcement_title.'.'.$model->file->extension;

   }

   $model->save();

 }

}



or announcement_image isn’t declared in your model / db table.

It’s hard to tell without having the exact error.

Looks like $model->file is null. Check validation rules and $_FILES array after upload.

Thanks for your replies. I’m out right now. I will be back at it soon.

Ok. Sorry for the delay. The following information will be a result of the code posted from skworden that contains the if statement in the controller. I don’t understand why announcement_image is 0. Thanks for any additional light you shed on this.


 public function actionCreate()

    {

        $model = new Announcements();


        if ($model->load(Yii::$app->request->post())) 

		{                                                            

			$imageName = $model->announcement_title;

			$model->file = UploadedFile::getInstance($model,'file');

		 if($model->file){	

			$model->announcement_image = $imageName.'.'.$model->file->extension;

			

				}

			

			$model->save();	

                        $model->file->saveAs( '../../uploads/'.$imageName.'.'.$model->file->extension );	


                          return $this->redirect(['view', 'id' => $model->announcement_id]);

		} else {

		    return $this->render('create', [

			'model' => $model,

		    ]);

        }

    }


	

I believe the following is what Bizley is referring to:


$_FILES = [

    'Announcements' => [

       

        'error' => [

            'announcement_image' => 0,

        ],

       

    ],

];

Rules:


public function rules()

    {

        return [

            [['announcement_title', 'announcement'], 'required'],

            [['announcement_title', 'announcement_image'], 'string', 'max' => 40],

            [['announcement'], 'string', 'max' => 2000],

	    [['file'], 'file', 'extensions'=>'jpg, gif, png, pdf'], 

	    ];

    }

Table Structure:


CREATE TABLE IF NOT EXISTS `announcements` (

`announcement_id` int(11) NOT NULL,

  `announcement_title` varchar(40) NOT NULL,

  `announcement` varchar(2000) NOT NULL,

  `announcement_image` varchar(40) NOT NULL

) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

What is the name of the file field in the form? Looks like you named it ‘announcement_image’ while you check the field named ‘file’.

Bizley, You are all over it. I changed the announcement_image to ‘file’ in the form & life is good. Thank you both very much for your expertise and patience.