php - include the related entities values in the result of a doctrine query -
i'm doing doctrine query, , results include properties of queried entity, not doesn't "follow" , fetch values of related entities.
e.g. have query inside of 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 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 , relationships work filtering data on queries, example.
but results $query->getquery()->getarrayresult()
expression not include data these relations.
it wont include ids fields. e.g., 1 record include these fields:
- id
- alt
- nombre"
- nombrealt
- descripcion
- poblacion
- vacantes
- horarioini
- horariofin
- url
- tag
- creado
- caducidad
- modificado
- eliminado
- activa
which valid fields, not include fields many-to-one relationship.
how can include these values query, , query only? e.g. without changing entities definition all queries affected?
update:
turns out query ok, , values alright. getresultarray()
messing results separating each set of values on different rows. need change approach result associative array, query ok. meh.
just looking at: $query->select('o');
i'd (think of in terms of sql) selecting. commas in there. i'd try this:
$query->select('o', 'p', 'c');
what trying first time should have been array. not comma delimited string showed. see source:
$selects = is_array($select) ? $select : func_get_args();
so either array or list of arguments should work.
update:
maybe missing from
? totally missed that. here i'll share code know works. maybe see something. pretty sure need from
clause , multiple obj references on 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 , 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 null'); $qb->orderby('aimg.lotnumber', 'asc'); $results = $qb->getquery()->getresult();
wiki
Comments
Post a Comment