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

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改