1

I've been looking to a solution to my problem for a while without success so I'm asking here.

How can we return a json-encoded result on an array of objects (or just an object) containing private properties ?

Indeed, when you use json_encode($myObject), it won't display the private or protected properties, which are present everywhere in the model when using Symfony...

I'm surprised I couldn't find any method like json_encode that would call getters instead of properties themselves.

Any idea ?

EDIT

In that case I would rather do a unique function that looks like :

public function toArray() {
    $vars = get_object_vars($this);
    $result = array();
    foreach ($vars as $key => $value) {
        if (is_object($value)) {
            $result[$key] = toArray($value);
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}

in order to avoid rewriting every property name everytime...

But anyway I think I'll just create an array containing the vars I need, so that I won't touch the model (which is generated code).

1
  • 1
    Please use the title field to describe your question. Commented Dec 5, 2011 at 15:28

2 Answers 2

2

Have you try GetSetMethodNormalizer ? http://api.symfony.com/2.0/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.html Ex. https://stackoverflow.com/a/6709828/520114

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

Comments

0

Right now there is no way for this. Only php serialize/unserialize handles the true serialisation of objects.

You'll have to implement them yourselve, or rather let objects return their json values themselves.

You will have to implement your own method toArray() where you expose all your private values in an array:

public function toArray()
{
  return array(
      'property1' => $this->myproperty1,
      'property2' => $this->myproperty2
  );
}

And call it like this:

json_encode(MyObject->toArray());

[Edit: this question is not about doctrine, but since you mention both symfony2 and the model, you can consider using Array Hydration for your model: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#array-hydration ]

5 Comments

Got a message "calling undefined method toArray" when trying to call it.
Sorry, added a bit to the explanation
Ouch okay, not very convenient when you have to do it for every model (with a lot of properties)...
Yes, that's true, but it's a limitation of PHP. If you just need to send it through json, look at the array hydration I pasted. This is the general PHP solution. For doctrine there are probably better solutions.
PHP 5.4 will bring support for a toJSON() method in objects when using json_encode(). The Zend_Json::encode() method of the Zend Framework supports the toJson() method of objects but is restricted to one level deep. So nested object structures are not supported and therefor you cant serialize an array of objects.

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.