Symfony 如何避免使用相关条令实体进行大量数据库查询?

Symfony 如何避免使用相关条令实体进行大量数据库查询?,symfony,doctrine-orm,Symfony,Doctrine Orm,在profiler工具栏上,我注意到很多db查询,400+,当我打印相关的实体信息时会发生这种情况。我对在我的实体中设置条令映射信息非常陌生,因此我不知道这是否是我错误配置的内容,或者这是一种方式,或者可能有更好的方式 基本上,估价实体与客户实体之间有两种一一对应的关系。我会发布我的代码,也许你可以发现一些东西,或者你可以告诉我,到目前为止,我的代码是否朝着正确的方向发展 估计类: 下面是CustomersController.php中的操作方法 public function viewActi

在profiler工具栏上,我注意到很多db查询,400+,当我打印相关的实体信息时会发生这种情况。我对在我的实体中设置条令映射信息非常陌生,因此我不知道这是否是我错误配置的内容,或者这是一种方式,或者可能有更好的方式

基本上,估价实体与客户实体之间有两种一一对应的关系。我会发布我的代码,也许你可以发现一些东西,或者你可以告诉我,到目前为止,我的代码是否朝着正确的方向发展

估计类:

下面是CustomersController.php中的操作方法

public function viewAction($customersId)
{
    $em = $this->getDoctrine()->getManager();
    $customer = $em->getRepository('MGAdminBundle:Customers')->find($customersId);
    if (!$customer){
        throw $this->createNotFoundException($this->get('translator')->trans('No record found for this customer.'));
    }

    $allHomes = $em->getRepository('MGAdminBundle:CustomersHomes')->getHomes($customersId,null,10);
    $billingHomes = $em->getRepository('MGAdminBundle:CustomersHomes')->getHomes($customersId, 'billing', 2);
    $allHomesCount = $em->getRepository('MGAdminBundle:CustomersHomes')->getHomesCountByCustomer($customersId);
    $customerSecondaryEmails = $customer->getEmails();
    $customerMessages = $customer->getMessages();
    $estimateRequests = $customer->getRequests();
    $estimates = $customer->getEstimates();
    return $this->render('MGAdminBundle:Customers:view.html.twig', array(
            'customer' => $customer,
            'allHomes' => $allHomes,
            'billingHomes' => $billingHomes,
            'allHomesCount' => $allHomesCount,
            'customerSecondaryEmails' => $customerSecondaryEmails,
            'messages' => $customerMessages,
            'estimateRequests' => $estimateRequests,
            'estimates' => $estimates,
            ));
}
这是MGAdminBundle:Customers:view.html.twig

{% extends 'MGAdminBundle::layout.html.twig' %}
{% block title 'Profile: ' | trans ~ customer.firstname | upper ~ ' ' ~ customer.lastname | upper %}
{% block content %}
    {% include 'MGAdminBundle:Customers/Partials:_customer-details.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_secondary-contacts.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_addresses.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_estimate-requests.html.twig' %}    
    {% include 'MGAdminBundle:Customers/Partials:_messages.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_estimates.html.twig' %}
{% endblock %}
{% if estimates|length > 0 %}
    <h3>{{ 'Estimates' | trans }}</h3>
    <fieldset>

    {% for estimate in estimates %}        
        <div class="row {{ cycle(['even','odd'],loop.index) }}">
            <div class="col-sm-1">{{ estimate.estimatesId | default('-') }}</div>
            <div class="col-sm-2">{% include 'MGAdminBundle:Customers/Partials:_estimate-address.html.twig' with {'homeAddress': estimate.homeAddress, 'workAddress': estimate.workAddress} %}</div>            
            <div class="col-sm-1"></div>
            <div class="col-sm-1"></div>
            <div class="col-sm-1">{{ estimate.total }}</div>
            <div class="col-sm-1">{{ estimate.status | default('-') }}</div>
            <div class="col-sm-3">payments</div>
            <div class="col-sm-1">options</div>
        </div>
    {% endfor %}
    </fieldset>
{% endif %}
这里是MGAdminBundle:Customers/Partials:_assessments.html.twig

{% extends 'MGAdminBundle::layout.html.twig' %}
{% block title 'Profile: ' | trans ~ customer.firstname | upper ~ ' ' ~ customer.lastname | upper %}
{% block content %}
    {% include 'MGAdminBundle:Customers/Partials:_customer-details.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_secondary-contacts.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_addresses.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_estimate-requests.html.twig' %}    
    {% include 'MGAdminBundle:Customers/Partials:_messages.html.twig' %}
    {% include 'MGAdminBundle:Customers/Partials:_estimates.html.twig' %}
{% endblock %}
{% if estimates|length > 0 %}
    <h3>{{ 'Estimates' | trans }}</h3>
    <fieldset>

    {% for estimate in estimates %}        
        <div class="row {{ cycle(['even','odd'],loop.index) }}">
            <div class="col-sm-1">{{ estimate.estimatesId | default('-') }}</div>
            <div class="col-sm-2">{% include 'MGAdminBundle:Customers/Partials:_estimate-address.html.twig' with {'homeAddress': estimate.homeAddress, 'workAddress': estimate.workAddress} %}</div>            
            <div class="col-sm-1"></div>
            <div class="col-sm-1"></div>
            <div class="col-sm-1">{{ estimate.total }}</div>
            <div class="col-sm-1">{{ estimate.status | default('-') }}</div>
            <div class="col-sm-3">payments</div>
            <div class="col-sm-1">options</div>
        </div>
    {% endfor %}
    </fieldset>
{% endif %}
下面是一个屏幕截图:

问题:

获得每个估价的家庭地址和工作地址的最佳策略是什么? 是否应该从存储库方法获取属于客户的估计?现在,估算是使用客户实体中的getter获得的。
请容忍我,我对这一切都是新手,我甚至不知道我是否有道理。谢谢。

使用条令,您可以使用..找到所有估算值

$repository = $this->getDoctrine()->getManager()
                            ->getRepository('MGAdminBundle:Customers');
要么

$estimates = $repository->findAll();
// Find all estimates on DB
或者

从那里可以看到整棵树。所以

foreach ($estimates as $estimate) {
    $things = $estimate->getThings();
    foreach ($things as $thing) {
       .. something with $thing ..
    }
}
或者用树枝

return $this->render('MGAdminBundle:Estimates:view.html.twig', array(
        'estimates' => $estimates,
    );
然后是真正的树枝

{% for estimate in estimates %}
    {% for thing in estimate.things %}
        .. something with {{ thing }} ..
    {% else %}
        No things // Twig has a nice incorporated for, else thing
    {% endfor %}
{% else %}
    No estimates
{% endfor %}
{% for estimate in estimates %}
    {% for thing in estimate.things %}
        .. something with {{ thing }} ..
    {% else %}
        No things // Twig has a nice incorporated for, else thing
    {% endfor %}
{% else %}
    No estimates
{% endfor %}