blank page on unique composite key validation

I has model1, Model2, Model3.

Model3 is a bridge table of model1 and Model2.

Model3 has composite primary key id_m1, id_m2.

I use unique rule as below in Model3.

[‘id_m1’, ‘unique’, ‘targetAttribute’ => [‘id_m1’, ‘id_m2’]]

In view page of item of Model1, i insert code for index page of Model3. Clicking create button on this index of Model3 section will open a form for Model3 which i copy from view folder of Model3 paste into Model1 view folder, and edit to be useable with Model1 controller. In Model1 controller i create new action to handle it. I also create a new view page for Model3 in Model1 view folder and an action for it in Model1 controller.

If i save Model3 with unique composite primary key for example first record will has id_m1 = 1 and id_m2 = 1, it is ok. If i save a duplicate, for example 2nd record with id_m1 = 1 and id_m2 = 1 again, it will return a blank white page. Just blank, no menu or yii2 footer.

Any idea what cause this?

If i remove the unique rule, then the page will be ok.

I alredy test with a single model has composite key such as that and implement same unique rule, everything is ok. So there must be something i do wrong when combine those 2 model in same controller.

Here is the create action code for Model3 that i insert in controller for Model1.


public function actionCreateModel3($id_m1)

    {        

        $model = new Model3();

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

            $model->id_m1 = $id_m1;

            if($model->save()){

                return $this->redirect(['model3-view', 'id_m2' => $model->id_m2, 'id_m1' => $model->id_m1]);

            }

        } else {

            $user = Yii::$app->user->getId();

            $staff= Staff::find()->where(['fk_user_id' => $user])->one();

            $model3object = Model3::find()->where(['staff'=>$staff->id])->all();

            $projectitem = Project::find()->all();

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

                'model' => $model,

                'id_m2' => $model3object,

                'project' => $projectitem ,

            ]);

        }

    }

I has found the cause, but cant find solution.


public function actionCreateModel3($id_m1)   <--- that parameter doesnt receive value

    {        

        $model = new Model3();

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

            $model->id_m1 = $id_m1; <--- replace this with number like 2 then will be fine

            if($model->save()){

But this only happen when duplicate composite key. If the code is wrong then it should show blank page when data is not duplicate as well.

Ok i has found the real cause of error, and understand the blank white screen got nothing to do with yii2, it is php doing.

In my search on the internet i found many suggestion to set php to show error or read php error log. I fail to get php to show error, so i read error log.

Php say there is error

PHP Parse error: syntax error, unexpected ‘::’ (T_PAAMAYIM_NEKUDOTAYIM)

on line 175 which the end of } of my else statement.

So i remember i didnt remove the else as show in tutorial. I remove it and problem solve, my fault for not following tutorial properly because previously i thought there is no difference has else and no else over there and everything work fine untill this error. Now when i analize properly yes there is big difference.


public function actionCreateModel3($id_m1)

    {        

        $model = new Model3();

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

            $model->id_m1 = $id_m1;

            if($model->save()){

                return $this->redirect(['model3-view', 'id_m2' => $model->id_m2, 'id_m1' => $model->id_m1]);

            }

        } //else {  <--- comment this

            $user = Yii::$app->user->getId();

            $staff= Staff::find()->where(['fk_user_id' => $user])->one();

            $model3object = Model3::find()->where(['staff'=>$staff->id])->all();

            $projectitem = Project::find()->all();

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

                'model' => $model,

                'id_m2' => $model3object,

                'project' => $projectitem ,

            ]);

        //} <--- comment this

    }

problem solved

when i recall back what i found as i describe in my second entry, it feel so weird. How do not removing else relate to parameter not receive value. :o