Creating default object from empty value

I have this error in my ActionCreate Controller

7652

empty.PNG

Controller




    public function actionCreate()

    {

        $modelCategory = new GradeCategories;

        $modelsItem = [new GradeItems];

        $userid = Yii::$app->getid->getId();              


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


            $modelsItem = Model::createMultiple(GradeItems::classname());

            Model::loadMultiple($modelsItem, Yii::$app->request->post());

            

                        //ajax validation

                        if (\Yii::$app->request->isAjax) {

                            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                            return \yii\helpers\ArrayHelper::merge(

                                    \yii\widgets\ActiveForm::validateMultiple($modelsItem), 

                                    \yii\widgets\ActiveForm::validate($modelCategory)

                            );

                    }else{

                        echo 'jkk-lms 3';

                    }             


            // validate all models

            $valid = $modelCategory->validate();

            $valid = Model::validateMultiple($modelsItem) && $valid;

            

            $modelCateory->created_by = Yii::$app->getid->getId();

            $modelCategory->created_at = new \yii\db\Expression('NOW()');


            if ($valid) {

                $transaction = \Yii::$app->db->beginTransaction();


                try {

                    if ($flag = $modelCategory->save(false)) {

                        foreach ($modelsItem as $modelItem) {

                            $modelItem->grade_category_id = $modelCategory->id;

                            if (! ($flag = $modelItem->save(false))) {

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }


                    if ($flag) {

                        $transaction->commit();

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

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

        }


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

            'modelCategory' => $modelCategory,

            'modelsItem' => (empty($modelsItem)) ? [new GradeItems] : $modelsItem

        ]);

    }



The surprise is that ActionUpdate is working perfectly

See it below




    public function actionUpdate($id)

    {

        $modelCategory = $this->findModel($id);

        $modelsItem = $modelCategory->gradeItem;


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


            $oldIDs = ArrayHelper::map($modelsItem, 'id', 'id');

            $modelsItem = Model::createMultiple(GradeItems::classname(), $modelsItem);

            Model::loadMultiple($modelsItem, Yii::$app->request->post());

            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsItem, 'id', 'id')));


            // validate all models

            $valid = $modelCategory->validate();

            $valid = Model::validateMultiple($modelsItem) && $valid;

            

            $modelCategory->updated_by = Yii::$app->getid->getId();

            $modelCategory->updated_at = new \yii\db\Expression('NOW()');    

            

		if (Yii::$app->request->isAjax) {

                        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                        return ActiveForm::validate($modelCategory);

       		}            


            if ($valid) {

                $transaction = \Yii::$app->db->beginTransaction();

                try {

                    if ($flag = $modelCategory->save(false)) {

                        if (!empty($deletedIDs)) {

                            GradeItems::deleteAll(['id' => $deletedIDs]);

                        }

                        foreach ($modelsItem as $modelItem) {

                            $modelItem->grade_category_id = $modelCategory->id;

                            if (! ($flag = $modelItem->save(false))) {

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }

                    if ($flag) {

                        $transaction->commit();

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

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

        }


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

            'modelCategory' => $modelCategory,

            'modelsItem' => (empty($modelsItem)) ? [new GradeItems] : $modelsItem

        ]);

    }



Please how do I resolve the problem in the ActionCreate Controller.

Thanks

I resolved it on my own. The issue is that I typed $modelCateory instead of $modelCategory.

Thanks