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 ,
]);
}
}