public function actionCreateMessage() { $msg = new MsgDetails(); $model = new ConversationMsg(); if (isset($_POST['ConversationMsg']) && isset($_POST['MsgDetails'])) { $model->attributes = $_POST['ConversationMsg']; $msg->attributes = $_POST['MsgDetails']; if ($model->validate() || $msg->validate()) { $btn = $_REQUEST['btn']; if ($btn == 'company') { //echo "<pre>";print_r($_REQUEST); die; $email = $_REQUEST['ConversationMsg']['reciver_id']; //echo $email; $email = ltrim($email); $email = rtrim($email); $rec = ServiceUser::model()->findByAttributes(array('email' => $email)); //echo "fsfsf"."<pre>";print_r($rec);die; $companyRecvierId = $rec->id; //echo $companyRecvierId;die; $sender_id = Yii::app()->session['username']; $date = date('Y-m-d'); $model->date = $date; $model->sender_id = $sender_id; $model->sender_type = 'admin'; $model->reciver_id = $companyRecvierId; $model->reciver_type = 'company'; $model->save(false); $conversationId = Yii::app()->db->getLastInsertID(); $convMsg = ConversationMsg::model()->findByPk(array('id' => $conversationId)); //echo "<pre>";print_r($rec);die; $text = $_REQUEST['MsgDetails']['msg']; $msg->conversation_id = $conversationId; $msg->user_type = 'admin'; $msg->msg = $text; $msg->tomsg = $convMsg['reciver_id']; $date = date('Y-m-d'); $msg->date = $date; if ($msg->save(false)) { $this->redirect(array('messagelisting')); } } else { $email = $_REQUEST['ConversationMsg']['reciver_id']; //echo $email;die; $email = ltrim($email); $email = rtrim($email); $rec = CustomerUser::model()->findByAttributes(array('email' => $email)); //echo "fsfsf"."<pre>";print_r($rec);die; $companyRecvierId = $rec->id; //echo $companyRecvierId;die; $sender_id = Yii::app()->session['username']; // echo $sender_id;die; $date = date('Y-m-d'); $model->date = $date; $model->sender_id = $sender_id; $model->sender_type = 'admin'; $model->reciver_id = $companyRecvierId; $model->reciver_type = 'company'; $model->save(false); $conversationId = Yii::app()->db->getLastInsertID(); $convMsg = ConversationMsg::model()->findByPk(array('id' => $conversationId)); $text = $_REQUEST['MsgDetails']['msg']; $msg->conversation_id = $conversationId; $msg->user_type = 'admin'; $msg->msg = $text; $msg->tomsg = $convMsg['reciver_id']; $date = date('Y-m-d'); $msg->date = $date; if ($msg->save(false)) { $this->redirect(array('messagelisting')); } } } else { $errors = $model->getErrors(); //var_dump($errors); $err = $msg->getErrors(); //var_dump($err); } } $this->render('createmessage', array('model' => $model, 'msg' => $msg)); }