public function createDialog() { $required = array('interlocutor_id' => true); $interlocutor_id = $this->controller->getParams($required)['interlocutor_id']; $thread = new \MessagingThread(); $threadUser1 = new \MessagingThreadUser(); $threadUser1->user_id = \Yii::app()->user->id; $threadUser2 = new \MessagingThreadUser(); $threadUser2->user_id = $interlocutor_id; $thread->threadUsers = array($threadUser1, $threadUser2); if (!$thread->withRelated->save(true, array('threadUsers'))) { throw new \Exception(print_r($thread->errors, true)); } $thread->refresh(); $this->controller->data = $thread; }