大佬教程收集整理的这篇文章主要介绍了php – 在Symfony2 / Doctrine SQL中使用JOIN,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下关系:
用户< -1:n->个人资料< -n:m-> RouteGroup< -1:n->路线
我想制作一个列出特定用户可以访问的所有路由的DQl.
我可以使用以下代码获取此信息:
$usr = $this->container->get('security.context')->getToken()->getUser(); foreach ($usr->getProfiles() as $profilE){ foreach ($profile->getRoutegroups() as $routegroup){ var_dump($routegroup->getRoutes()->toArray()); } }
显而易见,我不能使用这个代码,否则我将超载我的服务器,LOl.
我尝试了以下方法:
DQL:
$em->createQuery('SELECT p FROM CRMCoreBundle:User u JOIN CRMCoreBundle:Profile p JOIN CRMCoreBundle:roleGroup rg JOIN CRMCoreBundle:role r WHERE u.id=:user') ->setParameter('user',$user->getId()) ->getResult();
QueryBuilder(我尝试使用u.profiles – 关系的名称而不是实体 – 但是这样也没有):
$em->createQueryBuilder() ->SELEct('r') ->from('CRMCoreBundle:User','u') ->innerJoin('u.profiles','p') ->where('u.id = :user_id') ->setParameter('user_id',$user->getId()) ->getQuery() ->getResult();
有人可以帮忙吗?
更新:我尝试了Zeljko的解决方案,并制作了这个脚本:
return $this->getEntitymanager() ->createQueryBuilder() ->SELEct('u,r') ->from('CRMCoreBundle:User','p') ->innerJoin('p.routegroups','rg') ->innerJoin('rg.routes','r') ->where('u.id = :user_id')->setParameter('user_id',$user->getId()) ->getQuery() ->getResult();
但是我收到这个错误:
The parent object of entity result with alias 'r' was not found. The parent alias is 'rg'.
如果我更改“ – >选择(‘u,r’)”到“ – >选择(‘r’)”我得到这个:
[Semantical Error] line 0,col -1 near 'SELECT r FROM': Error: CAnnot SELEct entity through identification variables without choosing at least one root entity alias.
return $this->getEntitymanager() ->createQueryBuilder() ->SELEct('r') ->from('CRMCoreBundle:route','r') ->innerJoin('r.routegroup','rg') ->innerJoin('rg.profiles','p') ->innerJoin('p.users','u') ->where('u.id = :user_id') ->setParameter('user_id',$user->getId()) ->getQuery() ->getResult();
以上是大佬教程为你收集整理的php – 在Symfony2 / Doctrine SQL中使用JOIN全部内容,希望文章能够帮你解决php – 在Symfony2 / Doctrine SQL中使用JOIN所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。