Reputation: 47349
I'm doing a Doctrine query, and the results include the properties of the queried entity, but not it doesn't "follow" and fetch the values of the related entities.
E.g. I have this query inside of the OfertaRepository
:
$query = $this->createQueryBuilder('o');
$query->select('o');
$query->leftJoin(Pais::class, 'p', 'with', 'o.idPais = p.id');
$query->leftJoin( Contrato::class, 'c', 'with', 'o.idTipoContrato = c.id');
$query->andWhere('p.nombreCorto = :pais');
$query->andWhere('o.activa = 1');
$query->andWhere('o.eliminado is NULL');
$query->andWhere('o.caducidad > :hoy');
$query->setParameter('pais', $pais)->setParameter('hoy', new \DateTime());
return $query->getQuery()->getArrayResult();
The entity Oferta
has:
/**
* @var \Application\Entity\Pais
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Pais")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_pais", referencedColumnName="id", nullable=true)
* })
*/
private $idPais;
/**
* @var \Application\Entity\TipoContrato
*
* @ORM\ManyToOne(targetEntity="Application\Entity\TipoContrato")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_tipo_contrato", referencedColumnName="id", nullable=true)
* })
*/
private $idTipoContrato;
All referenced tables ( tipo_contrato
, pais
, etc) exist and the relationships work for filtering data on queries, for example.
But my results from the $query->getQuery()->getArrayResult()
expression do not include data from these relations.
It wont even include the ids from this fields. E.g., for any one record it will only include these fields:
Which are all valid fields, but do not include any fields that are a many-to-one relationship.
How can I include these values for this query, and for this query only? E.g. without changing the entities definition so all queries are affected?
Upvotes: 2
Views: 1891
Reputation: 7054
Just looking at: $query->select('o');
I'd say (think of it in terms of SQL) that is all you are selecting. Get some commas in there. I'd try something like this:
$query->select('o', 'p', 'c');
What I was trying to do first time should have been an array. Not a comma delimited string like I showed. See source:
$selects = is_array($select) ? $select : func_get_args();
So either an array or a list of arguments should work.
Update:
Maybe you are just missing the from
? Totally missed that. Here I'll share some code that I know works. Maybe you will see something. Pretty sure you just need a from
clause and the multiple obj references on the select
part.
$qb = $this->_em->createQueryBuilder();
$qb->select('aimg', 'ai');
$qb->from('OAS\Entity\AuctionImage', 'aimg');
$qb->leftJoin('aimg.auctionItem', 'ai', \Doctrine\ORM\Query\Expr\Join::WITH, 'ai.lotNumber = aimg.lotNumber AND ai.auction = aimg.auction');
$qb->where('aimg.auction = :auction')->setParameter('auction', $this->_auction);
$qb->andWhere('aimg.isDeleted = :boolFalse')->setParameter('boolFalse', 0);
$qb->andWhere('aimg.auctionItem IS NULL');
$qb->orderBy('aimg.lotNumber', 'asc');
$results = $qb->getQuery()->getResult();
Upvotes: 2