symfony - Named query not found -
in symfony controller, have following function:
protected function getdisplaytags(expertise $expertise, person $user){ $rep = $this->getdoctrine()->getrepository('expertisedefaultbundle:tag'); $q = $rep->createnamedquery('fetchtagsbyexpertise'); //problem here $results = $q->setparameters(array('person'=>$user->getid()), array('expertise'=>$expertise->getid()))->getresult(); return $results; }
which throws error: no query found named 'fetchtagsbyexpertise' on class 'expertise\defaultbundle\entity\tag'.
repository generic 1 provided doctrine.
i defined named native query fetchtagsbyexpertise
via annotation in tag entity:
namespace expertise\defaultbundle\entity; use symfony\component\validator\constraints assert; use doctrine\orm\mapping orm; /** * @orm\entity * @orm\table(name="tag") * @orm\namednativequeries({ * @orm\namednativequery( * name = "fetchtagsbyexpertise", * resultsetmapping= "mappingtagsbyexpertise", * query ="select t.id t_id, t.tag t_tag, a.expertise_id a_expertise_id, count(a.person_id) tagcount, sum(case when a.person_id = :person 1 else 0 end) has_curuser expertise_tags join tag t on (a.tag_id = t.id) a.expertise_id = :expertise group t.tag , a.expertise_id order t.tag" * ), * }) * @orm\sqlresultsetmappings({ * @orm\sqlresultsetmapping( * name = "mappingtagsbyexpertise", * entities= { * @orm\entityresult( * entityclass = "__class__", * fields = { * @orm\fieldresult(name = "id", column="t_id"), * @orm\fieldresult(name = "tag", column="t_tag"), * } * ), * @orm\entityresult( * entityclass = "associatedtag", * fields = { * @orm\fieldresult(name = "expertise_id", column="a_expertise_id"), * } * ) * }, * columns = { * @orm\columnresult("tagcount"), * @orm\columnresult("has_cursuser") * } * ) *}) */ class tag { //vanilla doctrine entity. no foreign keys // ... }
the joined entityclass, associatedtag
has composite primary key consisting of (tag_id, expertise_id, person_id)
. have tried including rest of keys in query , mapping has no effect.'
is there wrong how or i've defined named native query? why can't repository find it?
the right way call createnativenamedquery instead of createnamedquery.
protected function getdisplaytags(expertise $expertise, person $user){ $rep = $this->getdoctrine()->getrepository('expertisedefaultbundle:tag'); $q = $rep->createnativenamedquery('fetchtagsbyexpertise'); // no problem here ;) $results = $q->setparameters(array('person'=>$user->getid()), array('expertise'=>$expertise->getid()))->getresult(); return $results; }
Comments
Post a Comment