如何避免与相关学说实体进行大量数据库查询?(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:
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
最满意答案
是的,只需实例化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 ...
-
如何创建文件格式?(How are file formats created? If it is all binary how does encoding change the file type?)[2024-03-25]
决定什么格式的主要方式是通过文件扩展名或MIME类型 - 而不常用“幻数”。 文件扩展名将由操作系统或应用程序进行检查,以决定如何处理它(运行哪个应用程序或执行哪个代码部分)。 MIME类型用于扩展名(或文件名)并不总是适用的地方 - 例如,当通过HTTP下载文件时,文件的URI可能类似~.php?id=12973 。 文件类型不能单独确定,但HTTP协议将发送“Content-Type”定义来说明文件的格式,并且浏览器将正确处理它。 例如:Content-Type:image / png会强制浏览器将文件 ... -
嵌入未知的文件格式(Embed unknown file formats)[2023-10-14]
是的,只需实例化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 ... -
点云文件格式(PCL)(Point Clouds File Formats (PCL))[2023-07-31]
欢迎来到迷人的点云世界! 如果你喜欢Python,欢迎来自https://github.com/daavoo/pyntcloud 。 /结束招聘。 什么是最常用的点云文件格式? 一般pourpose 3D文件格式 嗯......几乎任何一般的pourpose 3D文件格式都隐含地支持点云,因为这些格式通常存储一堆多边形 ,并且为了定义多边形,您必须定义它的顶点 。 如果仔细考虑,那些顶点只是点云 。 一个很好的例子是.ply文件格式。 在许多摄影测量 (从现实世界生成点云的一种方法)软件中,导出点云是常见的 ... -
OBJ文件不同的格式(OBJ File Different Formats)[2023-07-20]
来自维基百科的面部元素 - 使用顶点,纹理和法线索引列表定义面。 缺少的第二个元素是纹理索引,指向要使用的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 ... -
.MAP文件格式的未知标志(Unknown flag in .MAP file format)[2021-08-09]
正如在这篇博文中暗示的那样,“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 ...