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 getCommentsLink() { return $this->hasOne(CommentsLink::className(), ['comments_id' => 'id']); }