How to do Innerjoin with related table?

Asked

Viewed 56 times

0

Guys I got a class Arquivo who has a Manytoone relationship with a class Usuario working right. I need to list all the data that are in the class Arquivos including relationships. How I do that in Doctrine?

Class relationship File is so:

/**
 * @ORM\ManyToOne(targetEntity="JN\Entity\Usuarios\Usuario", cascade={"persist"})
 * @ORM\JoinColumn(name="usuario_id", referencedColumnName="id", onDelete="SET NULL")
 **/
private $usuario;

When I do so on Doctrine I don’t return the content of the relationship:

$ent = $this->em->getRepository( "JN\Entity\Arquivos\Arquivo" )
                ->createQueryBuilder('t')
                ->getQuery()
                ->getArrayResult();

1 answer

0


Mount your query as follows:

$ent = $this->em->getRepository("JN\Entity\Arquivos\Arquivo" )
    ->createQueryBuilder('t')
    ->join('t.usuario', 'u')
    ->getQuery()
    ->getResult();

The result will be a collection of entities of the type JN\Entity\Arquivos\Arquivo. To iterate through these entities and catch the user of each file do so:

foreach ($ent as $arquivo) {
    $arquivo->getUsuario();
}
  • Thank you gave it right.

Browser other questions tagged

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