Пример #1
0
 /**
  * @param SiteModels $forum
  * @return boolean
  */
 public function create($forum)
 {
     // very, so much, stupid source code :)
     if ($this->validate()) {
         $user = Yii::$app->getUser()->getIdentity();
         // create post
         $post = new PostModels();
         $post->topic_id = 0;
         $post->message = $this->message;
         $post->save();
         if ($post->save()) {
             // create topic
             $topic = new TopicModels();
             $topic->forum_id = $forum->id;
             $topic->subject = $this->subject;
             $topic->post = $post;
             $topic->save();
             // update post.topic_id
             $post->link('topic', $topic);
             // update forum information
             $forum->updateCounters(['number_topics' => 1]);
             $forum->last_post_created_at = time();
             $forum->last_post_user_id = $post->id;
             $forum->last_post_username = $user->username;
             $forum->save();
             $this->topic = $topic;
             return true;
         }
     }
 }