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).

Yoot
  • 657
  • 2
  • 14
  • 26

2 Answers2

2

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

Community
  • 1
  • 1
webda2l
  • 6,686
  • 2
  • 26
  • 28
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 ]

Arend
  • 3,741
  • 2
  • 27
  • 37
  • Got a message "calling undefined method toArray" when trying to call it. – Yoot Dec 05 '11 at 15:03
  • Sorry, added a bit to the explanation – Arend Dec 05 '11 at 15:08
  • Ouch okay, not very convenient when you have to do it for every model (with a lot of properties)... – Yoot Dec 05 '11 at 15:11
  • 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. – Arend Dec 05 '11 at 15:13
  • 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. – ProTom Dec 05 '11 at 15:35