public function actionIndex($id = null) { $model = $id === null ? new Product() : $this->findModel($id); if (!empty($model->details)) { $model->titleVi = $model->details[0]->title; $model->titleEn = $model->details[1]->title; $model->thumbnailVi = $model->details[0]->thumbnail; $model->thumbnailEn = $model->details[1]->thumbnail; $model->summaryVi = $model->details[0]->summary; $model->summaryEn = $model->details[1]->summary; $model->contentVi = $model->details[0]->content; $model->contentEn = $model->details[1]->content; } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { $listItem = Product::find()->with('details')->all(); $category = Category::find()->with('details')->all(); $listCate = []; foreach ($category as $cate) { $listCate[$cate->id] = $cate->details[0]->title . ' / ' . $cate->details[1]->title; } return $this->render('index', ['model' => $model, 'listItem' => $listItem, 'listCate' => $listCate]); } }
protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }