<tfoot id='qhswQ'></tfoot>
<legend id='qhswQ'><style id='qhswQ'><dir id='qhswQ'><q id='qhswQ'></q></dir></style></legend>

  • <i id='qhswQ'><tr id='qhswQ'><dt id='qhswQ'><q id='qhswQ'><span id='qhswQ'><b id='qhswQ'><form id='qhswQ'><ins id='qhswQ'></ins><ul id='qhswQ'></ul><sub id='qhswQ'></sub></form><legend id='qhswQ'></legend><bdo id='qhswQ'><pre id='qhswQ'><center id='qhswQ'></center></pre></bdo></b><th id='qhswQ'></th></span></q></dt></tr></i><div id='qhswQ'><tfoot id='qhswQ'></tfoot><dl id='qhswQ'><fieldset id='qhswQ'></fieldset></dl></div>

  • <small id='qhswQ'></small><noframes id='qhswQ'>

        <bdo id='qhswQ'></bdo><ul id='qhswQ'></ul>

      1. 在 Symfony2/Doctrine SQL 中使用 JOIN

        时间:2024-08-09
        • <i id='wLdhU'><tr id='wLdhU'><dt id='wLdhU'><q id='wLdhU'><span id='wLdhU'><b id='wLdhU'><form id='wLdhU'><ins id='wLdhU'></ins><ul id='wLdhU'></ul><sub id='wLdhU'></sub></form><legend id='wLdhU'></legend><bdo id='wLdhU'><pre id='wLdhU'><center id='wLdhU'></center></pre></bdo></b><th id='wLdhU'></th></span></q></dt></tr></i><div id='wLdhU'><tfoot id='wLdhU'></tfoot><dl id='wLdhU'><fieldset id='wLdhU'></fieldset></dl></div>

          <small id='wLdhU'></small><noframes id='wLdhU'>

              <tbody id='wLdhU'></tbody>
            <legend id='wLdhU'><style id='wLdhU'><dir id='wLdhU'><q id='wLdhU'></q></dir></style></legend>

            1. <tfoot id='wLdhU'></tfoot>
                <bdo id='wLdhU'></bdo><ul id='wLdhU'></ul>

                  本文介绍了在 Symfony2/Doctrine SQL 中使用 JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在尝试使用 QueryBuilder 或 DQL 时遇到问题.

                  I have a problem while trying to USE QueryBuilder OR DQL.

                  我有以下关系:

                  用户 <-1:n-> 配置文件 <-n:m-> RouteGroup <-1:n-> Route

                  User <-1:n-> Profile <-n:m-> RouteGroup <-1:n-> Route

                  我想制作一个 DQL,列出特定用户可以访问的所有路由.我可以使用以下代码获取此信息:

                  I would like to make a DQL that lists all the routes that a specific user has access. I can get this information with the following code:

                  $usr = $this->container->get('security.context')->getToken()->getUser();
                  foreach ($usr->getProfiles() as $profile){
                      foreach ($profile->getRoutegroups() as $routegroup){
                          var_dump($routegroup->getRoutes()->toArray());
                       }
                  }
                  

                  出于显而易见的原因,我不能使用此代码,否则我的服务器会超载,哈哈.

                  For obvious reason i cant use this code, otherwise I will overload my server, 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 - 关系的名称而不是实体的名称 - 但这也不起作用):

                  QueryBuilder (i tried using u.profiles - the name of the relationship instead of the entity - but this did not work also):

                  $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', 'u')
                          ->innerJoin('u.profiles','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'.
                  

                  如果我将->select('u, r')"更改为->select('r')",我会得到:

                  If i change "->select('u, r')" to "->select('r')" i get this:

                  [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.
                  

                  推荐答案

                  在尝试了一些替代方案后,我发现我可以进行反向查找,从路由到用户.解决方法如下:

                  After trying some alternatives I found out that I could make an inverse lookup, starting from routes to users. The solution was as follows:

                  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();
                  

                  这篇关于在 Symfony2/Doctrine SQL 中使用 JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Doctrine2 ORM 选择更新 下一篇:Doctrine 实体删除 vs 删除查询,性能比较

                  相关文章

                • <tfoot id='1S7I2'></tfoot>

                      <bdo id='1S7I2'></bdo><ul id='1S7I2'></ul>
                    1. <small id='1S7I2'></small><noframes id='1S7I2'>

                      <i id='1S7I2'><tr id='1S7I2'><dt id='1S7I2'><q id='1S7I2'><span id='1S7I2'><b id='1S7I2'><form id='1S7I2'><ins id='1S7I2'></ins><ul id='1S7I2'></ul><sub id='1S7I2'></sub></form><legend id='1S7I2'></legend><bdo id='1S7I2'><pre id='1S7I2'><center id='1S7I2'></center></pre></bdo></b><th id='1S7I2'></th></span></q></dt></tr></i><div id='1S7I2'><tfoot id='1S7I2'></tfoot><dl id='1S7I2'><fieldset id='1S7I2'></fieldset></dl></div>

                      <legend id='1S7I2'><style id='1S7I2'><dir id='1S7I2'><q id='1S7I2'></q></dir></style></legend>