sql - Join 3 tables in Symfony2 -
i have problem query in symfony2, have 3 entites (they simplified bit):
class grouping { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @var string * * @orm\column(name="name", type="string", length=255) */ protected $name; /** @orm\onetomany(targetentity="groupmember", mappedby="family") */ protected $groupmembers; class groupmember { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(name="group_member_type", type="integer") */ protected $groupmembertype; /** * @orm\manytoone(targetentity="group", inversedby="groupmembers") * @orm\joincolumn(name="group_id", referencedcolumnname="id", nullable=false) */ protected $group; /** * @orm\manytoone(targetentity="test\userbundle\entity\user", inversedby="memberships") * @orm\joincolumn(name="user_id", referencedcolumnname="id", nullable=false) */ protected $user;
user -> standard fosuserbundle user
i want create query returns groups current user grouping member. tell me how that?
here try:
select g (group g inner bring together groupmember gm on g.id=gm.group_id) inner bring together fos_user u on gm.user_id = u.id u.id = 6
but returns
unknown column 'g' in 'field list'
using query builder, assuming you've fetched user , stored in $userobject
, bundle name bundlename
:
$groups = $this->getdoctrine()->getmanager()->createquerybuilder() ->select('g') ->from('bundlename:group', 'g') ->innerjoin('g.groupmembers', 'gm') ->innerjoin('gm.user', 'u') ->where('u = :user') ->setparameter('user', $userobject) ->getquery() ->getresult();
you need create sure relational mappings correct.
/** @orm\onetomany(targetentity="groupmember", mappedby="family") */ protected $groupmembers;
should be
/** @orm\onetomany(targetentity="groupmember", mappedby="group") */ protected $groupmembers;
sql symfony2 doctrine2
No comments:
Post a Comment