简体   繁体   中英

include the related entities values in the result of a doctrine query

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.

Eg 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. Eg, for any one record it will only include these fields:

  • id
  • alt
  • nombre"
  • nombreAlt
  • descripcion
  • poblacion
  • vacantes
  • horarioIni
  • horarioFin
  • url
  • tag
  • creado
  • caducidad
  • modificado
  • eliminado
  • activa

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? Eg without changing the entities definition so all queries are affected?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM