/** * Updates an existing Content model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = Content::find()->with('translations')->where(['id' => $id])->one(); if ($model === null) { throw new NotFoundHttpException('The requested page does not exist.'); } foreach (Yii::$app->request->post('ContentTranslation', []) as $language => $data) { foreach ($data as $attribute => $translation) { $model->translate($language)->{$attribute} = $translation; } } if (Yii::$app->request->post('ContentTranslation', []) && $model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Content::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ['id', 'status', 'key', 'created_at', 'updated_at', 'name' => ['asc' => ['name' => SORT_ASC], 'desc' => ['name' => SORT_DESC]]]]); $this->load($params); if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); if ($this->createdRange) { $dateArray = explode('-', $this->createdRange); $dateFrom = trim($dateArray[0]); $dateTo = trim($dateArray[1]); $query->andWhere('created_at <= :dateTo', [':dateTo' => $dateTo])->andWhere('created_at >= :dateFrom', [':dateFrom' => $dateFrom]); } else { $query->andFilterWhere(['created_at' => $this->created_at]); } if ($this->updatedRange) { $dateArray = explode('-', $this->updatedRange); $dateFrom = trim($dateArray[0]); $dateTo = trim($dateArray[1]); $query->andWhere('updated_at <= :dateTo', [':dateTo' => $dateTo])->andWhere('updated_at >= :dateFrom', [':dateFrom' => $dateFrom]); } else { $query->andFilterWhere(['updated_at' => $this->updated_at]); } $query->andFilterWhere(['like', 'key', $this->key]); $query->joinWith(['translations' => function ($query) { if ($this->name) { $query->andWhere(['like', 'name', $this->name]); } if ($this->content) { $query->andWhere(['like', 'content', $this->content]); } }]); return $dataProvider; }