Serialize Relationships Doctrine

Asked

Viewed 58 times

0

How do I do a json with Entity relationships in Doctrine? I’ve looked around and made a few attempts but nothing that has worked so far.

I’m doing so and returns the normal json(without the relationships):

$list = $this->getEm()->createquery('select u from Ticket\Entity\Ticket u');
$query = $list->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
           return new \Zend\View\Model\JsonModel($query);

Relationship of the Ticket with user class:

/**
 * @ORM\ManyToOne(targetEntity="Ticket\Entity\Usuario", inversedBy="ticket")
 * @ORM\JoinColumn(name="usuario_id", referencedColumnName="id")
 * 
 */ 
protected $usuario;

output:

[{"id":41,"description":"teste"},{"id":38,"description":"teste1"}]

Is there a "trick" to this? vlw

  • What do you want to do? Bring user information along with the ticket?

1 answer

0

You only need to bring the user along with the ticket when running the query. As for the rest of the code, nothing changes.

$list = $this
    ->getEm()
    ->createQuery('
        SELECT t, u
        FROM Ticket\Entity\Ticket t
        JOIN t.usuario u');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.