How to use getter method as property when I json_encode a Doctrine object?

For example, in class

/**
 * @Doctrine\ORM\Mapping\Entity
 */
class Example
{
    /**
     * @var int The id
     *
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\GeneratedValue
     * @Doctrine\ORM\Mapping\Column(type="integer")
     */
    public $id;

    /**
     * @var string
     *
     * @Doctrine\ORM\Mapping\Column(type="string")
     */
    public $name;


    public function getRandomNumber()
    {
        return rand();
    }
}

When I call json_encode()with this entity, it does not bring value getRandomNumber()as a property of the class.

I would like him to come back:

{
    "id": 1,
    "name": "foo",
    "randomNumber": 123456
}

But it returns:

{
    "id": 1,
    "name": "foo"
}
+4
source share
4 answers

You must use the JMSSerializer! I use it on all my sites, and this is a really great solution. https://jmsyst.com/libs/serializer It uses annotations and creates json-serialized objects. It accepts the attribute values ​​of your entity, but with a specific @Accessor annotation, it uses the specified getter to get its value. For instance:

use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Accessor;

/**
 * @Expose
 * @Accessor("getRandomNumber")
 */
public $randomNumber;

public function getRandomNumber(){
    return rand();
}
+1

\JsonSerializable, , JSON:

class Example implements \JsonSerializable
{
    // ...

    public function jsonSerialize()
    {
        // mixed data which can be serialized by json_encode() 
        return array(
            'id' => $this->id,
            'name' => $this->name,
            'randomNumber' => $this->getRandomNumber(),
        );
    }
}
+2

json_encodewill return a JSON object with the public properties of the object passed to it. If you want to enable randomNumber, you can make it public and set it in the constructor, for example

/**
 * @Doctrine\ORM\Mapping\Entity
 */
class Example
{
    /**
     * @var int The id
     *
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\GeneratedValue
     * @Doctrine\ORM\Mapping\Column(type="integer")
     */
    public $id;

    /**
     * @var string
     *
     * @Doctrine\ORM\Mapping\Column(type="string")
     */
    public $name;

    /**
     * @var int
     */
    public $randomNumber;

    public function __construct() {
      $this->randomNumber = $this->getRandomNumber();
    }

    public function getRandomNumber()
    {
        return rand();
    }
}

For more complex serialization of objects, you can see the symfony serializer

+1
source

Use virtual property

Information: https://jmsyst.com/libs/serializer/master/reference/annotations#virtualproperty

/**
 * @Doctrine\ORM\Mapping\Entity
 */
class Example
{
    /**
     * @var int The id
     *
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\GeneratedValue
     * @Doctrine\ORM\Mapping\Column(type="integer")
     */
    public $id;

    /**
     * @var string
     *
     * @Doctrine\ORM\Mapping\Column(type="string")
     */
    public $name;

    /**
     * @Serializer\VirtualProperty()
     */
    public function getRandomNumber()
    {
        return rand();
    }
}
+1
source

Source: https://habr.com/ru/post/1690811/


All Articles