Coeng
Coeng

Reputation: 59

Call to undefined method stdClass::save() in yii framework

I am building chats pop up where one user will initiate conversation with another user. In my initiatechat function i have the following error Call to undefined method stdClass::save().

Look at codes and find THIS SAVE FUNCTION ==>> the save() function that throw error.

If two users have already initiated conversation this error does not happen.

public function actionInitiatechat() {
        if (isset($_POST)){
            //$message = Myclass::checkPostvalue($_POST['message']) ? $_POST['message'] : "";
            $senderId = Myclass::checkPostvalue($_POST['sender']) ? $_POST['sender'] : "";
            $receiverId = Myclass::checkPostvalue($_POST['receiver']) ? $_POST['receiver'] : "";
            $messageType = Myclass::checkPostvalue($_POST['messageType']) ? $_POST['messageType'] : "";
            $sourceId = Myclass::checkPostvalue($_POST['sourceId']) ? $_POST['sourceId'] : "";
            $timeUpdate = time();
            $message = $_POST['message'];
            $Products = Products::model()->findByPk($sourceId);
            if(isset($Products) && $Products->approvedStatus == 0)
            {
                echo "error";
            }
            else
            {



    $criteria = new CDbCriteria;
                    $criteria->condition = "(user1 = '$senderId' AND user2 = '$receiverId') OR (user1 = '$receiverId' AND user2 = '$senderId')";

                    $chatModel = Chats::model()->find($criteria);

                    $encodeMsg = urlencode($message);

                    if (empty($chatModel)){
                        $newChat = new Chats();
                        $newChat->user1 = $senderId;
                        $newChat->user2 = $receiverId;
                        $newChat->lastMessage = $encodeMsg;
                        $newChat->lastToRead = $receiverId;
                        $newChat->lastContacted = $timeUpdate;

                        $newChat->save();

                        $criteria = new CDbCriteria;
                        $criteria->condition = "(user1 = '$senderId' AND user2 = '$receiverId') OR (user1 = '$receiverId' AND user2 = '$senderId')";

                        $chatModel = Chats::model()->find($criteria);
                    }
                    $chatModel->lastContacted = $timeUpdate;
                    if ($chatModel->user1 == $senderId){
                        $chatModel->lastToRead = $chatModel->user2;
                    }else{
                        $chatModel->lastToRead = $chatModel->user1;
                    }
                    $chatModel->lastMessage = $encodeMsg;
            THIS SAVE FUNCTION ==>>     $chatModel->save();

                    $messageModel = new Messages();
                    $messageModel->message = $encodeMsg;
                    $messageModel->messageType = $messageType;
                    $messageModel->senderId = $senderId;
                    $messageModel->sourceId = $sourceId;
                    $messageModel->chatId = $chatModel->chatId;
                    $messageModel->createdDate = $timeUpdate;
                    $messageModel->save();              
                }

                    echo "success";
            }
            }
            else
            {
            echo "failed";
            }
        }

Upvotes: 0

Views: 918

Answers (1)

Matthew Turland
Matthew Turland

Reputation: 763

The statement below is presumably returning a "bare" object of type stdClass, which doesn't define a save() method, hence your error.

$chatModel = Chats::model()->find($criteria);

Run var_dump($chatModel); immediately after this statement and see what type of object you're getting.

Upvotes: 1

Related Questions