首页 \ 问答 \ 如何避免与相关学说实体进行大量数据库查询?(How can I avoid a gazillion database queries with related doctrine entities?)

如何避免与相关学说实体进行大量数据库查询?(How can I avoid a gazillion database queries with related doctrine entities?)

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

基本上, Estimates实体与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 %}

这是一个截图: 截图

问题:

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

请耐心等待,我对这一切都是新手,我甚至不知道如果我有意义。 谢谢。


On the profiler toolbar I noticed a lot of db queries, 400+, this happens when I print a related entity info. I am very new to setting doctrine mapping information within my entities so I don't know whether it's something I have misconfigured or if that's the way it is or maybe there is a better way to do it.

Basically the Estimates entity has two one-to-one relations to the CustomersHomes entity. I'll post my code, maybe you can spot something or perhaps you could tell me if my code so far is going in the right direction.

The Estimates class:

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

...

}

And here is the action method in the 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,
            ));
}

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

And here is MGAdminBundle:Customers/Partials:_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 %}

And finally the partial to print the estimate address: MGAdminBundle:Customers/Partials:_estimate-address.html.twig (I'm still working with this template)

{% if homeAddress is defined %}

    {{ homeAddress.name }}

{% endif %}

{% if workAddress is defined %}

    {{ workAddress.name }}

{% endif %}

And here is a screenshot: Screenshot

Questions:

  • What is the best strategy to get the home address and work address of each estimate?
  • Should the estimates that belong to a customer be fetched from a repository method? Right now the estimates are gotten using a getter inside the customer entity.

Please bear with me, I'm new to all of this and I don't even know If I make sense. Thank you.


原文:https://stackoverflow.com/questions/21070469
更新时间:2022-03-28 21:03

最满意答案

是的,只需实例化MyZip并转换为ByteArray。

Embed[(source="mzip.zip", mimeType="application/octet-stream")]
public static var MyZip:Class;

var zip:ByteArray = new MyZip() as ByteArray;

Yes, just instantiate MyZip and cast as a ByteArray.

Embed[(source="mzip.zip", mimeType="application/octet-stream")]
public static var MyZip:Class;

var zip:ByteArray = new MyZip() as ByteArray;

相关问答

更多
  • 不需要调用guess_formats只需使用parse_date_time : parse_date_time(sampleDates, c("Ymd", "mdY")) [1] "2004-04-06 UTC" "2004-04-06 UTC" "2004-04-06 UTC" "2004-04-07 UTC" "2004-04-06 UTC" [6] "2004-04-07 UTC" "2014-06-28 UTC" "2014-06-30 UTC" "2014-07-12 UTC" "2014 ...
  • 错误很明显: HttpBearerAuth类没有属性formats因此以下行不应该在HttpBearerAuth配置下: 'formats' => ['application/json' => Response::FORMAT_JSON,], 这是\ yii \ filters \ ContentNegotiator的属性 。 有关更多详细信息,请参阅有关内容协商的文档 The error is clear: the HttpBearerAuth class has no property formats ...
  • 虽然我不会说它是标准化的,但是有一些语料库经常用于比较不同的压缩算法。 例如检查卡尔加里语料库或坎特伯雷语料库。 即使您选择了大量文件,您选择哪种文件类型也很重要,因为压缩率会根据实际数据与压缩算法假设的基础模型的拟合程度而有所不同。 检查此站点和此站点以查看不同类型数据的压缩结果的比较。 Although I wouldn't say it is standarised, there are some corpus that are often used to compare different comp ...
  • 好吧,它不能打开Apple列出的那些格式。 WebView处理Web格式:HTML,CSS,JS,SVG等,而不是文档格式。 Well, it can open none of those formats that Apple lists. WebView handles Web formats: HTML, CSS, JS, SVG, etc., not document formats.
  • 单元格格式为货币或自定义,因此您无法在那里找到欧元符号。 检查InStr() ,它会检查单元格.Value ,而不是.Format 。 要在Cell中找到Eur,请检查以下格式: If Cells(NewLastRowNumber, 10).NumberFormat = "€0.00" Then 要在VBA中查看确切的数字格式,请选择具有所需格式的单元格并运行以下命令: Sub Test() Debug.Print Selection.NumberFormat End Sub The cell ...
  • 决定什么格式的主要方式是通过文件扩展名或MIME类型 - 而不常用“幻数”。 文件扩展名将由操作系统或应用程序进行检查,以决定如何处理它(运行哪个应用程序或执行哪个代码部分)。 MIME类型用于扩展名(或文件名)并不总是适用的地方 - 例如,当通过HTTP下载文件时,文件的URI可能类似~.php?id=12973 。 文件类型不能单独确定,但HTTP协议将发送“Content-Type”定义来说明文件的格式,并且浏览器将正确处理它。 例如:Content-Type:image / png会强制浏览器将文件 ...
  • 是的,只需实例化MyZip并转换为ByteArray。 Embed[(source="mzip.zip", mimeType="application/octet-stream")] public static var MyZip:Class; var zip:ByteArray = new MyZip() as ByteArray; Yes, just instantiate MyZip and cast as a ByteArray. Embed[(source="mzip.zip", mimeTy ...
  • 欢迎来到迷人的点云世界! 如果你喜欢Python,欢迎来自https://github.com/daavoo/pyntcloud 。 /结束招聘。 什么是最常用的点云文件格式? 一般pourpose 3D文件格式 嗯......几乎任何一般的pourpose 3D文件格式都隐含地支持点云,因为这些格式通常存储一堆多边形 ,并且为了定义多边形,您必须定义它的顶点 。 如果仔细考虑,那些顶点只是点云 。 一个很好的例子是.ply文件格式。 在许多摄影测量 (从现实世界生成点云的一种方法)软件中,导出点云是常见的 ...
  • 来自维基百科的面部元素 - 使用顶点,纹理和法线索引列表定义面。 缺少的第二个元素是纹理索引,指向要使用的vt行。 要在obj导出中获取此数据,您需要打开对象以创建uv并在导出时启用包含UV 。 From wikipedia under face elements - Faces are defined using lists of vertex, texture and normal indices. the missing second element is the texture index, poi ...
  • 正如在这篇博文中暗示的那样,“f”意味着“功能”。 顺便说一下,没有第2部分。 我可以将“i”反向工程为“内联”,这是COMDAT部分中的一个功能。 比较这两个声明: class test_vtable_printf { public: test_vtable_printf() {} // Decorated with "f" and "i" in the map file }; VS: class test_vtable_printf { public: test_vtable_p ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)