gpt4 book ai didi

mysql - Magento 产品标题 char_length 限制

转载 作者:行者123 更新时间:2023-11-29 12:35:29 25 4
gpt4 key购买 nike

我正在尝试检索产品标题(名称)超过 N 个字符的产品集合。到目前为止,我正在尝试这样做:

 $collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name');

$collection->getSelect()->where("CHAR_LENGTH(`e.name`) > 70");

它将输出的查询是:

SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (CHAR_LENGTH(`e.name`) > 70)

它当前抛出一个错误,因为它不理解名称列实际上是什么

Column not found: 1054 Unknown column 'e.name' in 'where clause'

我可以循环遍历所有产品来检查字符长度,但这效率不高。我试图找到一个纯粹的 MySQL 解决方案,而不需要写出 ->query(...)。

最佳答案

根据我必须自己进行的连接,事实证明这有点棘手。我知道我必须运行的 SQL,它看起来像这样:

SELECT value, CHAR_LENGTH( value ) AS 'length'
FROM catalog_product_entity_varchar
WHERE entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product'))
AND CHAR_LENGTH(value) > 70;

magento 方式:

    //calculate attribute id of title
$attr = Mage::getSingleton("eav/config")->getAttribute('catalog_product', 'entity_type_id')->getEntityTypeId();
$col = Mage::getModel('eav/entity_attribute')
->getCollection()
->addFieldToFilter('attribute_code', 'name')
->addFieldToFilter('entity_type_id', $attr);
$attr_id = $col->getFirstItem()->getAttributeId();

//get products
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
->join( array('cat_varchar'=> 'catalog_product_entity_varchar'), 'cat_varchar.entity_id = e.entity_id', array('cat_varchar.value'))
->where("`cat_varchar`.`attribute_id` = {$attr_id}")
->where("CHAR_LENGTH(`cat_varchar`.`value`) > 70");

关于mysql - Magento 产品标题 char_length 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com