如何调试Magento2里product load()的sql

3.40K 浏览开发笔记

如何调试Magento2里product load()的sql

调试getCollection数据集的sql代码,很好调试。直接这样写:

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); $productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToSelect('name'); echo $collection->getSelect();//这里输出sql

一般显示

SELECT `e`.* FROM `catalog_product_entity` AS `e`

但是如何调试load($id)这种的sql呢?

比如读取id为40的产品数据

$product = $objectManager->get('MagentoCatalogModelProductFactory')->create()->load(40);

这种就很麻烦了,涉及到N层调用,要找N个文件才行。

1, MagentoCatalogModelResourceModelProduct.php 里的load()方法

2,MagentoFrameworkEntityManagerEntityManager.php 里的load()方法

3,MagentoFrameworkEntityManagerOperationRead.php 里的execute()方法

里面的 

$entity = $this->readAttributes->execute($entity, $arguments);

4,MagentoFrameworkEntityManagerOperationReadReadAttributes.php 里的execute()方法

里面的

$entityData = array_merge($entityData, $action->execute($entityType, $entityData, $arguments));

5,MagentoEavModelResourceModelReadHandler.php里的execute()方法

里面的

$connection->fetchAll($unionSelect);

这里的$unionSelect才是最终获取产品数据的sql

我们调试下

$unionSelect = new MagentoFrameworkDBSqlUnionExpression(                 $selects,                 MagentoFrameworkDBSelect::SQL_UNION_ALL ); echo $unionSelect;

得到的sql语句为

(SELECT `t`.`value`, `t`.`attribute_id` FROM `catalog_product_entity_int` AS `t` WHERE (entity_id = '40') AND (`store_id` IN (1, 0)) ORDER BY `t`.`store_id` DESC )UNION ALL(SELECT `t`.`value`, `t`.`attribute_id` FROM `catalog_product_entity_text` AS `t` WHERE (entity_id = '40') AND (`store_id` IN (1, 0)) ORDER BY `t`.`store_id` DESC )UNION ALL(SELECT `t`.`value`, `t`.`attribute_id` FROM `catalog_product_entity_varchar` AS `t` WHERE (entity_id = '40') AND (`store_id` IN (1, 0)) ORDER BY `t`.`store_id` DESC )UNION ALL(SELECT `t`.`value`, `t`.`attribute_id` FROM `catalog_product_entity_decimal` AS `t` WHERE (entity_id = '40') AND (`store_id` IN (1, 0)) ORDER BY `t`.`store_id` DESC )UNION ALL(SELECT `t`.`value`, `t`.`attribute_id` FROM `catalog_product_entity_datetime` AS `t` WHERE (entity_id = '40') AND (`store_id` IN (1, 0)) ORDER BY `t`.`store_id` DESC )

其实跟我们想的一样,他是读取的这5个表的数据,这5个表就是产品的eav表,存放属性值的表。

catalog_product_entity_int catalog_product_entity_text catalog_product_entity_varchar catalog_product_entity_decimal catalog_product_entity_datetime

另外,在mariadb数据库的UNINO ALL有些bug,会导致排序不准确。

最后于 7月前 被admin编辑 ,原因:
0