0

I have the following ajax code:

            $.ajax({
                type: 'post',
                url: '/conversation/test',
                data: { conversation: JSON.stringify(<?php echo json_encode($conversation) ?>) },
                success: function (response) {
                    console.log(response);
                }
            });

Now I have on my test.php:

<?php

    $conversation = json_decode($_POST['conversation']);


 ?>

<?php foreach ($conversation->getUsers() as $conUser) {
    // Skip current user
    if ($conUser->getId() == UserSession::getUserId()) {
       continue;
    } ?>
    <a href="/<?php echo $conUser->getUri(); ?>/"><?php echo $conUser->getName(); ?></a>
<?php } ?>

And my response on the console is:

<br />
<b>Fatal error</b>:  Call to undefined method stdClass::getUsers() in     <b>/Users/msalamanca/PhpStorm/pinporn/trunk/application/views/default/conversation/test.php</b> on line <b>8</b><br />

I don't understand what I am doing wrong here.

12
  • $conversation is just decoded JSON you passed to ajax? json_decode is just for converting JSON string to array - please review us3.php.net//manual/en/function.json-decode.php. If you want to use getter getUsers() you need to implement some class with properties and getters/setters. You cannot use getter on array. Commented Jun 16, 2014 at 18:07
  • @Cockootec Technically, json_decode returns an object by default, and only returns an array if you explicitly tell it to by using the second parameter. That being said, the sentiment of your post is correct. There is no way for PHP to know that the JSON should be converted into some class that has a specific method. Commented Jun 16, 2014 at 18:09
  • so what you are saying is that I can get the object back? Commented Jun 16, 2014 at 18:09
  • What is the value of $conversation here echo json_encode($conversation)? And what exactly is it that you're trying to accomplish? Commented Jun 16, 2014 at 18:15
  • 1
    json is for data. it's not a general serialization system. you cannot pass actual objects around, and json strings will decode only to a stdClass object (or array) in PHP Commented Jun 16, 2014 at 18:27

1 Answer 1

1

Finally I understood what I was doing wrong. So on my view, I did the following:

    <div id="messages-widget">

    </div>

    <!--  Updates the conversation display and check for new messages  -->
    <script>
        $(document).ready(function () {

            $.post( "/conversation/messages", { conId: <?php echo $conversation->getId(); ?> })
                 .done(function (data) {
                      $('#messages-widget').html(data);
            });


        });

    </script>

Then in my Controller I just did the following:

    public function messages($args=array()) {

        $conId = $args['conId'];

        // Get current conversation
        $conversation = ConversationModel::getConversationById($conId);

        DataHolder::getInstance()->addObject('conversation', $conversation);
    }

So like this, my view will have the conversation object that I wanted, with its properties. Doing it like this, I can use the getters and setters that I needed.

Sign up to request clarification or add additional context in comments.

2 Comments

That looks better! One thing you might need to take care of is to check wether the actual user is allowed to view the conversion requested.
Yeah I just removed a bunch of code from the answer, just to show the idea :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.