Пример #1
0
 /**
  * @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;
 }
Пример #2
0
 /**
  * @param Topic $topic
  * @return boolean
  */
 public function create($topic)
 {
     if ($this->validate()) {
         $user = Yii::$app->getUser()->getIdentity();
         $post = new Post();
         $post->topic_id = $topic->id;
         $post->message = $this->message;
         $post->save();
         $topic->updateCounters(['number_posts' => 1]);
         $topic->last_post_username = $user->username;
         $topic->last_post_created_at = time();
         $topic->last_post_id = $post->id;
         $topic->last_post_user_id = $user->id;
         $topic->save();
         $this->post = $post;
         return true;
     }
     return false;
 }