/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CommentsFields::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['comments_id' => $this->comments_id, 'field_name' => $this->field_name]); $query->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
/** * Finds the CommentsFields model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $comments_id * @param integer $field_name * @return CommentsFields the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($comments_id, $field_name) { if (($model = CommentsFields::findOne(['comments_id' => $comments_id, 'field_name' => $field_name])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getCommentsFields() { return $this->hasMany(CommentsFields::className(), ['comments_id' => 'id']); }
public function save($runValidation = true, $attributeNames = null) { $isNewRecord = $this->isNewRecord; $transaction = \Yii::$app->db->beginTransaction(); if (!parent::save($runValidation, parent::attributes())) { $transaction->rollback(); return false; } if ($isNewRecord) { $commentLink = new CommentsLink(); $commentLink->comments_id = $this->id; $commentLink->url = $this->url; $commentLink->link_params = $this->link_params; $commentLink->user_id = !\Yii::$app->user->isGuest ? \Yii::$app->user->id : null; if (!$commentLink->save()) { $transaction->rollback(); return false; } } else { CommentsFields::deleteAll(['comments_id' => $this->id]); } foreach ($this->fields as $field) { $name = $field['name']; if ($this->{$name} != null) { $model = new CommentsFields(); $model->comments_id = $this->id; $model->field_name = $field['name']; $model->value = $this->{$name}; if (!($st = $model->save($runValidation = true))) { $transaction->rollback(); return false; } } } $transaction->commit(); return true; }