/** * @return boolean */ public function create() { // very, so much, stupid source code :) if ($this->validate()) { $user = Yii::$app->getUser()->getIdentity(); // create post $post = new Post(); $post->topic_id = 0; $post->message = $this->message; $post->save(); if ($post->save()) { // create topic $topic = new Topic(); $topic->subject = $this->subject; $topic->post = $post; $topic->save(); // update post.topic_id $post->link('topic', $topic); $tagNames = explode(',', $this->tags); foreach ($tagNames as $tagName) { /** @var Tag $tagModel */ $tagModel = Tag::findOne($tagName); $topic->link('tags', $tagModel); } $this->topic = $topic; return true; } } return false; }
/** * Create post * @param integer $id * @return boolean */ public function create($id) { $user = Yii::$app->getUser()->getIdentity(); $topic = Topic::findOne($id); $this->_topic = $topic; $post = new Post(); $post->topic_id = $this->_topic->id; $post->message = $this->message; $post->save(); $this->_topic->updateCounters(['number_posts' => 1]); $this->_topic->last_post_id = $post->id; $this->_topic->last_post_user_id = $user->id; $this->_topic->last_post_created_at = time(); $this->_topic->last_post_username = $user->username; $this->_post = $post; if ($this->_topic->save()) { return true; } return false; }