protected function findModel($id) { if (($model = Comments::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function init() { parent::init(); if ($this->model == null) { $this->model = new \msdie\modules\comments\models\CommentForm(); // $this->model->url=\Yii::$app->request->absoluteUrl; } if ($this->conditionLinkParams == null) { $this->_comments = Comments::find()->where(['comments_link.url' => \Yii::$app->request->absoluteUrl, 'status_id' => 1])->joinWith('commentsLink')->all(); } else { $this->_comments = Comments::find()->where(['comments_link.link_params' => $this->conditionLinkParams, 'status_id' => 1])->joinWith('commentsLink')->all(); if ($this->linkParams == null) { $this->linkParams = is_array($this->conditionLinkParams) ? $this->conditionLinkParams[0] : $this->conditionLinkParams; } } $this->_action = Url::toRoute(['/comments/default/add', 'url' => \Yii::$app->request->absoluteUrl, 'link_params' => $this->linkParams]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Comments::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); $query->joinWith('commentsLink'); 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(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'status_id' => $this->status_id, 'parent_id' => $this->parent_id]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'text', $this->text])->andFilterWhere(['like', 'comments_link.url', $this->pageUrl]); return $dataProvider; }
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; }
/** * @return \yii\db\ActiveQuery */ public function getComments() { return $this->hasOne(Comments::className(), ['id' => 'comments_id']); }
public function rules() { $comments = new Comments(); return $comments->rules(); }