doukan4039 2015-07-29 17:54
浏览 43
已采纳

Symfony / Doctrine:如何减少SELECT查询的数量? (多级关联实体; twig + jsonSerialize())

Due to the answer of my last question I was able to reduce the SELECT-queries on the first level. Unfortunately the associated Entities are linked deeper, e.g.:

Item -> Group -> Subscriber -> User -> username

Repository method:

// ItemRepository
public function findAll() {
    return $this->createQueryBuilder('item')
                ->addSelect('groups')->join('item.groups', 'groups')
                ->getQuery()->getResult()
}

twig template:

{% for item in items %}
    {# Level: 0 #}
    Name: {{ item.name }}<br/>
    Groups:<br/>
    <ul>
        {# Level: 1 #}
        {% for group in item.groups %}
           <li>{{ group.name }}<br/>
               <ol>
               {# Level: 2 #}
               {% for subscriber in group.subscribers %}
                   {# Level: 3 #}
                   <li>{{ subscriber.user.username }}</li>
               {% endfor %}
               </ol>
           </li>
        {% endfor %}
    </ul>
{% endfor %}

Note: I'm using jsonSerialize to prepare the JSON-data, which includes multi-level iterating as well.

use JsonSerializable;
// ...

class Item implements JsonSerializable {

    // ...

    public function jsonSerialize() {
        $subscribers = array();
        $groups      = $this->getGroups();
        foreach ($groups as $group) {
            foreach ($group->getSubscribers() as $subscriber) {
                $subscribers[$subscriber->getId()] = array(
                    'userId'   => $subscriber->getUser()->getId();
                    'username' => $subscriber->getUser()->getUsername();
                );
            }
        }

        return array(
            'id'          => $this->getId(),
            'subscribers' => $subscribers
            // ...
        );
    }
}

Is there a way to join the deeper associated data as well to reduce the number of SELECT-queries once more (for twig and jsonSerialize())?

  • 写回答

1条回答 默认 最新

  • dscc90150010 2015-07-29 18:27
    关注

    I suggest you to change fetch mode in the specific query, as described here in the doc.

    So you can describe your query as follow:

    $qb =  $this->createQueryBuilder('item')
                    ->addSelect('groups')->join('item.groups', 'groups'); // Not necessary anymore
    
            $query = $qb->getQuery();
            // Describe here all the entity and the association name that you want to fetch eager
            $query->setFetchMode("YourBundle\\Entity\\Item", "groups", ClassMetadata::FETCH_EAGER);
            $query->setFetchMode("YourBundle\\Entity\\Groups", "subscriber", ClassMetadata::FETCH_EAGER);
            $query->setFetchMode("YourBundle\\Entity\\Subscriber", "user", ClassMetadata::FETCH_EAGER);
            ...
    
    return $qb->->getResult();
    

    NB:

    Changing the fetch mode during a query is only possible for one-to-one and many-to-one relations.

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?