用sytrfony中的教义和fosrestbundle冲洗后的未知查询

时间:2015-11-09 09:14:59

标签: symfony doctrine-orm doctrine fosrestbundle jmsserializerbundle

谢谢你帮助我,这已经过了一个星期,我已经陷入了一种不明智的学说行为和骗局中的困境。

我有2个具有多对多关系的实体:产品和类别

自从上周我在productRESTcontroller上发送一个补丁请求来更新实体之间的关系后,一切都运行良好,在刷新后,通常它会重新命名我更新的实体。 但现在,它更新了实体,但它也产生了许多" SELECT"查询,我从未做过。

  • 请求是用guzzle创建和执行的,它是一个承诺(异步请求)
  • 使用voryxRestGeneratorBundle创建productRESTcontroller,使用fosrestbundle和jmsserializerbundle

我删除了doctrine缓存,symfony缓存等,尝试了许多其他方法来更新实体,但没有解决这个问题。

我可以在日志中看到:

[2015-11-09 09:26:58] doctrine.DEBUG: "START TRANSACTION" [] []
[2015-11-09 09:26:58] doctrine.DEBUG: UPDATE Category SET name = ? WHERE id = ? ["EXPRESS",10152] []
[2015-11-09 09:26:58] doctrine.DEBUG: "COMMIT" [] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_prod_dist AS id_prod_dist2, t0.ref_prod AS ref_prod3, t0.on_sale AS on_sale4, t0.price AS price5, t0.ecotax AS ecotax6, t0.ean13 AS ean137, t0.active AS active8, t0.id_default_image AS id_default_image9, t0.name AS name10, t0.id_cats AS id_cats11, t0.id_combis AS id_combis12, t0.link_rewrite AS link_rewrite13, t0.weight_product AS weight_product14, t0.maj_auto AS maj_auto15 FROM Product t0 INNER JOIN product_category ON t0.id = product_category.product_id WHERE product_category.category_id = ? [10152] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [14] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_prod_dist AS id_prod_dist2, t0.ref_prod AS ref_prod3, t0.on_sale AS on_sale4, t0.price AS price5, t0.ecotax AS ecotax6, t0.ean13 AS ean137, t0.active AS active8, t0.id_default_image AS id_default_image9, t0.name AS name10, t0.id_cats AS id_cats11, t0.id_combis AS id_combis12, t0.link_rewrite AS link_rewrite13, t0.weight_product AS weight_product14, t0.maj_auto AS maj_auto15 FROM Product t0 INNER JOIN product_category ON t0.id = product_category.product_id WHERE product_category.category_id = ? [10180] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [12] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_prod_dist AS id_prod_dist2, t0.ref_prod AS ref_prod3, t0.on_sale AS on_sale4, t0.price AS price5, t0.ecotax AS ecotax6, t0.ean13 AS ean137, t0.active AS active8, t0.id_default_image AS id_default_image9, t0.name AS name10, t0.id_cats AS id_cats11, t0.id_combis AS id_combis12, t0.link_rewrite AS link_rewrite13, t0.weight_product AS weight_product14, t0.maj_auto AS maj_auto15 FROM Product t0 INNER JOIN product_category ON t0.id = product_category.product_id WHERE product_category.category_id = ? [10186] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [13] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_prod_dist AS id_prod_dist2, t0.ref_prod AS ref_prod3, t0.on_sale AS on_sale4, t0.price AS price5, t0.ecotax AS ecotax6, t0.ean13 AS ean137, t0.active AS active8, t0.id_default_image AS id_default_image9, t0.name AS name10, t0.id_cats AS id_cats11, t0.id_combis AS id_combis12, t0.link_rewrite AS link_rewrite13, t0.weight_product AS weight_product14, t0.maj_auto AS maj_auto15 FROM Product t0 INNER JOIN product_category ON t0.id = product_category.product_id WHERE product_category.category_id = ? [10192] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [1] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_prod_dist AS id_prod_dist2, t0.ref_prod AS ref_prod3, t0.on_sale AS on_sale4, t0.price AS price5, t0.ecotax AS ecotax6, t0.ean13 AS ean137, t0.active AS active8, t0.id_default_image AS id_default_image9, t0.name AS name10, t0.id_cats AS id_cats11, t0.id_combis AS id_combis12, t0.link_rewrite AS link_rewrite13, t0.weight_product AS weight_product14, t0.maj_auto AS maj_auto15 FROM Product t0 INNER JOIN product_category ON t0.id = product_category.product_id WHERE product_category.category_id = ? [10204] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [11] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.supplier_reference AS supplier_reference2, t0.price AS price3, t0.weight AS weight4, t0.colisage AS colisage5, t0.in_catalog AS in_catalog6, t0.product_id AS product_id7, t0.supplier_id AS supplier_id8 FROM ProductSupplier t0 WHERE t0.product_id = ? [11] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.refCombi AS refCombi2, t0.idProdAttrDist AS idProdAttrDist3, t0.ean13 AS ean134, t0.idProdDist AS idProdDist5, t0.weight AS weight6, t0.price AS price7, t0.ecotax AS ecotax8, t0.majAuto AS majAuto9, t0.erasable AS erasable10, t0.options_ids AS options_ids11, t0.image_ids AS image_ids12, t0.product_id AS product_id13 FROM Combination t0 WHERE t0.product_id = ? [11] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [17] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.supplier_reference AS supplier_reference2, t0.price AS price3, t0.weight AS weight4, t0.colisage AS colisage5, t0.in_catalog AS in_catalog6, t0.product_id AS product_id7, t0.supplier_id AS supplier_id8 FROM ProductSupplier t0 WHERE t0.product_id = ? [17] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.refCombi AS refCombi2, t0.idProdAttrDist AS idProdAttrDist3, t0.ean13 AS ean134, t0.idProdDist AS idProdDist5, t0.weight AS weight6, t0.price AS price7, t0.ecotax AS ecotax8, t0.majAuto AS majAuto9, t0.erasable AS erasable10, t0.options_ids AS options_ids11, t0.image_ids AS image_ids12, t0.product_id AS product_id13 FROM Combination t0 WHERE t0.product_id = ? [17] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [25] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.supplier_reference AS supplier_reference2, t0.price AS price3, t0.weight AS weight4, t0.colisage AS colisage5, t0.in_catalog AS in_catalog6, t0.product_id AS product_id7, t0.supplier_id AS supplier_id8 FROM ProductSupplier t0 WHERE t0.product_id = ? [25] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.refCombi AS refCombi2, t0.idProdAttrDist AS idProdAttrDist3, t0.ean13 AS ean134, t0.idProdDist AS idProdDist5, t0.weight AS weight6, t0.price AS price7, t0.ecotax AS ecotax8, t0.majAuto AS majAuto9, t0.erasable AS erasable10, t0.options_ids AS options_ids11, t0.image_ids AS image_ids12, t0.product_id AS product_id13 FROM Combination t0 WHERE t0.product_id = ? [25] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.id_cat_dist AS id_cat_dist2, t0.name AS name3, t0.active AS active4, t0.maj_auto AS maj_auto5, t0.erasable AS erasable6, t0.ids_products AS ids_products7, t0.url_cat AS url_cat8 FROM Category t0 INNER JOIN product_category ON t0.id = product_category.category_id WHERE product_category.product_id = ? [31] []
[2015-11-09 09:26:58] doctrine.DEBUG: SELECT t0.id AS id1, t0.supplier_reference AS supplier_reference2, t0.price AS price3, t0.weight AS weight4, t0.colisage AS colisage5, t0.in_catalog AS in_catalog6, t0.product_id AS product_id7, t0.supplier_id AS supplier_id8 FROM ProductSupplier t0 WHERE t0.product_id = ? [31] []

这6分钟,直到我收到严重错误消息:

[2015-11-09 09:32:02] php.CRITICAL: Fatal Error: Maximum execution time of 300 seconds exceeded {"type":1,"file":"/Applications/MAMP/htdocs/Symfony/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php","line":127,"level":-1,"stack":[]} []
[2015-11-09 09:32:02] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Maximum execution time of 300 seconds exceeded" at /Applications/MAMP/htdocs/Symfony/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php line 127 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 0): Error: Maximum execution time of 300 seconds exceeded at /Applications/MAMP/htdocs/Symfony/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php:127)"} []

提前感谢您的帮助

更新:我终于明白了这个问题。正如wilt告诉我的那样,实体是完全加载的,并且因为每个类别都与产品和产品相关,所以每次都会加载很多实体。

一个解决方案是将一些jmsserialzer注释特别是maxdepth(1)放到连接的实体上,并在te相关实体上指定一个排除状态,它现在可以完美地工作。

1 个答案:

答案 0 :(得分:0)

from the doctrine documentation

  

在许多情况下,实体之间的关联可能会变得非常大。甚至在像博客这样的简单场景中。如果帖子可以评论,你总是要假设一篇文章吸引了数百条评论。在Doctrine 2.0中,如果您访问了一个关联,它将始终完全加载到内存中。如果您的关联包含数百或数千个实体,这可能会导致严重的性能问题。

因此,如果您更新ManyToMany关系中的集合,Doctrine会在执行任何removeadd操作之前将集合中的所有元素加载到内存中。

标记为fetch="EXTRA_LAZY"可能对您没有帮助(因为您可能也会删除操作)。

我能想到的最佳解决方案是将ManyToMany关系拆分为OneToMany + ManyToOne关联,其中包含名为ProductCategoryLink的实体:

Product - ProductCategoryLink - Category

然后可以通过查找相应的ProductCategoryLink来完成添加或删除,而无需从数据库中解析所有其他记录(您不想更改的记录)。