doctrine2 - Symfony 2.8 : DQL query to display an arraycollection -
i'm beginner on symfony 2 , have problem dql query : i'm trying display property arraycollection.
here's case : 2 classes joined "manytoone" , "onetomany"
dishgrade.php :
/** * @var dish $dish * * @orm\manytoone(targetentity="dish", inversedby="grades", cascade={"persist", "remove", "merge"}) * @orm\joincolumn(name="dish_id", referencedcolumnname="id", nullable=false) * */ private $dish;
dish.php :
/** * @var \doctrine\common\collections\collection $grades * * @orm\onetomany(targetentity="dishgrade", mappedby="dish", cascade={"all"}, orphanremoval=true) */ private $grades;
then here's controller returns array of objects "dishgrade"
/** * grades dish. * * * @apidoc( * output = "awadac\apibundle\model\dish", * statuscodes = { * 200 = "returned when successful", * 404 = "returned when dish not found" * } * ) * * @get("/dishes/{id}") * * @param request $request request object * @param int $id dish id * * @return array * * @throws notfoundhttpexception when dish not exist */ public function getdishaction(request $request, $id) { $dish = $this->getdishmanager()->getrepository()->getgrades($id); if (!$dish) { throw $this->createnotfoundexception('dish not exist.'); } return $dish; }
and here's query doesn't work :
dishrepository.php :
public function getgrades($id){ $qb = $this->_em->createquerybuilder(); return $qb->select('d.grades') ->from('awadacapibundle:dish', 'd') ->where('d.id = :id') ->setparameter('id', $id) ->getquery() ->getresult(); }
the error code :
{"code":500,"message":"[semantical error] line 0, col 9 near 'grades awadacapibundle:dish': error: invalid pathexpression. must statefieldpathexpression.","errors":null}
and when want properties object "dish" works, wonder why can't display arraycollection alone.
thank time !
if in dishrepository able create query builder
$qb = $this->createquerybuilder('d');
further remove ->from() statement , guess should work properly:
return $qb->select('d.grades') ->where('d.id = :id') ->setparameter('id', $id) ->getquery() ->getresult();
Comments
Post a Comment