[英]Magento unknown column in having clause
我需要在Magento自定义集合网格中使用having子句,我尝试这样做:
$store = Mage::app()->getStore($this->getStore());
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('small_image')
->addAttributeToFilter('status',1);
$collection->joinTable(array('table_url' => 'thebot_url'),"table_url.product_id = entity_id",
array(
"urls" => new Zend_Db_Expr('group_concat(table_url.url)'),
"competitor_price" => new Zend_Db_Expr('group_concat(table_url.competitor_price)'),
'rdy_shipping' => 'rdy_shipping',
'competitor_shipping_price' => 'competitor_shipping_price',
'min_sell_price' => new Zend_Db_Expr('min(ROUND((table_url.competitor_price + table_url.competitor_shipping_price),2))'),
'rdy_price' => new Zend_Db_Expr('round(((IF(at_price.value_id > 0, at_price.value, at_price_default.value)) + min(table_url.rdy_shipping)),2)')
), null,'left');
if ($store->getId())
{
$collection->addStoreFilter($store);
$collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
$collection->joinAttribute('manufacturer', 'catalog_product/manufacturer', 'entity_id', null, 'left', $store->getId());
}
else
{
$collection->addAttributeToSelect('price');
$collection->addAttributeToSelect('status');
$collection->addAttributeToSelect('manufacturer');
$collection->addAttributeToSelect('visibility');
}
$collection->addAttributeToFilter('visibility', array("neq" => 1));
$collection->getSelect()->group("e.entity_id");
$collection->getSelect()->having("rdy_price > min_sell_price AND min_sell_price > 0");
此错误显示:
找不到列:1054'having子句'中的未知列'rdy_price'
sql查询是:
SELECT COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`) AND (`at_status`.`attribute_id` = '80') AND (`at_status`.`store_id` = 0)
INNER JOIN `catalog_product_website` AS `product_website` ON product_website.product_id = e.entity_id AND product_website.website_id = '10'
INNER JOIN `catalog_product_entity_varchar` AS `at_custom_name_default` ON (`at_custom_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_custom_name_default`.`attribute_id` = '56') AND `at_custom_name_default`.`store_id` = 0
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`entity_id` = `e`.`entity_id`) AND (`at_visibility_default`.`attribute_id` = '85') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND (`at_visibility`.`attribute_id` = '85') AND (`at_visibility`.`store_id` = '16') WHERE (at_status.value = '1') AND (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) != 1) HAVING (rdy_price > min_sell_price AND min_sell_price > 0)
那么为什么magento没有将表“table_url”加入到这个请求中(我认为这可以用于计算结果)?
使用此代码:
$min_sell_price = 'min(ROUND((table_url.competitor_price + table_url.competitor_shipping_price),2))';
$rdy_price = 'round(((IF(at_price.value_id > 0, at_price.value, at_price_default.value)) + min(table_url.rdy_shipping)),2)';
$collection->getSelect()->having("{$rdy_price} > {$min_sell_price} AND {$min_sell_price} > 0");
首先,您的查询失败的原因是:没有GROUP BY
,但查询中有HAVING
。
非常可疑 。
起初我认为你的代码应该更像下面的代码,而$collection->getSelect()
部分看起来会被覆盖。
$collection->getSelect()->group("e.entity_id")->having("rdy_price > min_sell_price AND min_sell_price > 0");
实际上,更多的研究了解到,不是你提供的查询失败了,而是Magento的内置getSelectCountSql()
。
关于GROUP BY HAVING在Magento Collection上遇到问题的这篇文章的人似乎也遇到了同样的问题。
他的解决方案是扩展getSelectCountSql()
我在自定义集合类中创建了一个扩展的getSelectCountSql(),它在having语句所需的缺失列中添加。
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
// Adding some custom features
[...]
}
我们必须跟踪方法: Varien_Db_Select::getSelectCountSql()
。 幸运的是,有人已经为我们这样做了 。
使用
HAVING
加载集合工作正常,但如果您在网格上下文中使用它,Magento可能会调用集合上的getSelectCountSql()
来获取修改后的查询以检索结果数。我不知道为什么这不包含在默认实现中,但实际上它意味着你不能对计算列使用HAVING但必须重复表达式:
原帖
也许您应该看一下有关joinTable的类似主题
特别是以下部分 - 为什么我认为您的查询不起作用:
- 表很简单,它是magento
namespace/entity
格式,您可以在配置,资源模型和集合中使用它。 您可以使用格式数组的数组('alias'=> 'namespace / entity' )
我很害怕你的表thebot_url是一个自定义表。 因此,您需要添加资源模型。
这是一个如何将其添加到模块的示例
<?xml version="1.0"?>
<config>
...
<!-- The module you are using this query on -->
<global>
...
<models>
<thebot_url>
<class>Custom_TheBotUrl_Model</class>
<resourceModel>thebot_url_mysql4</resourceModel>
</thebot_url>
<thebot_url_mysql4>
<class>Custom_TheBotUrl_Model_Mysql4</class>
<entities>
<myfirsttable>
<table>thebot_url</table>
</myfirsttable>
</entities>
</thebot_mysql4>
</models>
...
</global>
...
</config>
所以现在你有了一个命名空间thebot_url
和一个实体myfirsttable
,这将导致magento选择<table>thebot_url</table>
总之:在joinTable()
的第一个参数( $table
)中,这个例子的是thebot_url/myfirsttable
有关更多详细信息,您可能需要阅读本文,了解如何使用Magento的自定义表创建自定义模块 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.