如何避免与相关学说实体进行大量数据库查询?

时间:2014-01-12 02:10:13

标签: symfony doctrine-orm

在探查器工具栏上,我注意到了很多数据库查询,400 +,当我打印相关的实体信息时会发生这种情况。我很擅长在我的实体中设置教义映射信息,所以我不知道它是否是我错误配置的东西,或者它是否是它的方式或者可能有更好的方法。

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

估算类:

class Estimates
{
...
 /**
 * @ORM\OneToOne(targetEntity="MG\AdminBundle\Entity\CustomersHomes")
 * @ORM\JoinColumn(name="work_address_id", referencedColumnName="homes_id")
 *
 */
private $workAddress;

/**
 *
 * @ORM\OneToOne(targetEntity="MG\AdminBundle\Entity\CustomersHomes")
 * @ORM\JoinColumn(name="homes_id", referencedColumnName="homes_id")
 *
 */
private $homeAddress;

...

// getters and setters are in place below

...

}

这是CustomersController.php中的action方法

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:客户: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 %}

以下是MGAdminBundle:客户/部分:_estimates.html.twig

{% 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:_estimate-address.html.twig(我还在使用这个模板)

{% if homeAddress is defined %}

    {{ homeAddress.name }}

{% endif %}

{% if workAddress is defined %}

    {{ workAddress.name }}

{% endif %}

这是一个截图: Screenshot

问题:

  • 获取每项估算的家庭住址和工作地址的最佳策略是什么?
  • 是否应从存储库方法中获取属于客户的估算值?现在,使用客户实体内部的getter获得估算值。

请忍受我,我是所有这一切的新手,我甚至不知道我是否有意义。谢谢。

1 个答案:

答案 0 :(得分:0)

使用Doctrine,您可以使用..

找到所有估算值
$repository = $this->getDoctrine()->getManager()
                            ->getRepository('MGAdminBundle:Customers');

要么...

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

或者...

$estimates = $repository->findBy(array(
    'constraint1' => 'blah1',
    'constraint2' => 'blah2',
));
// Find all estimates that fit constraints
// Constraints can take object if they are associations

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

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 %}