2012-03-30 145 views
0

我试图让所有的产品存储在某个类别下。

一切工作正常书写:Magento:无效的属性名称错误

$category = Mage::getModel('catalog/category')->load($categoryId); 

$products = Mage::getModel('catalog/product') 
       ->getCollection() 
       ->addCategoryFilter($category) 
       ->setOrder('position', 'DESC') 
       ->load(); 

我已经得到了所有的产品通过正确的位置属性来分类的。
因为他们中的一些不具有任何位置设置,我想将它们排除在外,所以我想简单地说:

$category = Mage::getModel('catalog/category')->load($categoryId); 

$products = Mage::getModel('catalog/product') 
       ->getCollection() 
       ->addCategoryFilter($category) 
       ->addFieldToFilter('position', array('gt'=>0)) 
       ->setOrder('position', 'DESC') 
       ->load(); 

但网页无法加载,我得到一个错误“出现了错误处理您的请求”和错误日志被存储在VAR /报告开始

一个:5:{I 0,S:33:“无效属性名:位置

和。我无法解决这个问题..我搜索了很多,并尝试了很多的解决方案,但没有人工作。
我不明白为什么它接受一个属性排序,而不是过滤。
任何想法?

编辑:
由$产品进行查询是

SELECT 
    `e`.*, 
    `cat_index`.`position` AS `cat_index_position`, 
    `price_index`.`price`, 
    `price_index`.`tax_class_id`, 
    `price_index`.`final_price`, 
    IF(`price_index`.`tier_price`, LEAST(`price_index`.`min_price`, `price_index`.`tier_price`), 
    `price_index`.`min_price`) AS `minimal_price`, 
    `price_index`.`min_price`, 
    `price_index`.`max_price`, 
    `price_index`.`tier_price` 

FROM 
    `tb_catalog_product_entity` AS `e` 

INNER JOIN 
    `tb_catalog_category_product_index` AS `cat_index` 
ON 
    cat_index.product_id=e.entity_id 
AND 
    cat_index.store_id='3' 
AND 
    cat_index.category_id='2476' 
AND 
    cat_index.is_parent=1 

INNER JOIN 
    `tb_catalog_product_index_price` AS `price_index` 
ON 
    price_index.entity_id = e.entity_id 
AND 
    price_index.website_id = '1' 
AND 
    price_index.customer_group_id = 0 

ORDER BY 
    `cat_index_position` ASC, 
    `cat_index`.`product_id` ASC 


我也试过:

$category = Mage::getModel('catalog/category')->load($categoryId); 

$products = Mage::getModel('catalog/product') 
      ->getCollection() 
      ->addCategoryFilter($category) 
      ->addFieldToFilter('cat_index_position', 'notnull') 
      ->setOrder('position', 'ASC') 
      ->load(); 

具有相同的结果:

Invalid attribute name: cat_index_position.";i:1;s:4700:"#0 /path/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(1155): Mage::exception('Mage_Eav', 'Invalid attribu...') 



编辑:
我想我的问题是类似this one 但我无法弄清楚如何解决它!



EDITED:

还试图

$category = Mage::getModel('catalog/category')->load($categoryId); 

$products = Mage::getModel('catalog/product') 
      ->getCollection() 
      ->addAttributeToSelect('cat_index_position') 
      ->addAttributeToFilter('cat_index_position', array('gt'=>'0')); 

始终相同的误差



EDITED:

如果我添加

WHERE `position` > 0 

在我的查询浏览器,它的工作!
那么如何将它添加到我的查询内magento?

回答

0

到目前为止,我以这种方式解决:
1)修改
应用程序/代码/核心/Mage/Eav/Model/Entity/Abstract.php

~line 384 
if (empty($attributeInstance) 
|| !($attributeInstance instanceof Mage_Eav_Model_Entity_Attribute_Abstract) 
|| (!$attributeInstance->getId() && !in_array($attributeInstance->getAttributeCode(), $this->getDefaultAttributes())) 
) { 
    return false; 
} 

if (empty($attributeInstance) 
|| !($attributeInstance instanceof Mage_Eav_Model_Entity_Attribute_Abstract) 
// || (!$attributeInstance->getId() && !in_array($attributeInstance->getAttributeCode(), $this->getDefaultAttributes())) 
) { 
    return false; 
} 


2)改性
应用程序/代码/核心/法师/ EAV /型号/实体/收集/ Abstract.php

~line 1237 
if ($entity->isAttributeStatic($attribute)) { 
    $conditionSql = $this->_getConditionSql('e.'.$attribute, $condition); 
} else { 
.... 

if ($entity->isAttributeStatic($attribute)) { 
    $conditionSql = $this->_getConditionSql('e.'.$attribute, $condition); 

    $attributeInstance = Mage::getSingleton('eav/config') 
          ->getAttribute($this->getAttribute($attribute)->getEntityType(), $attribute); 

    if (!$attributeInstance->getId() && !in_array($attributeInstance->getAttributeCode(), $this->getAttribute($attribute)->getDefaultAttributes())) 
    { 
     // die("FOUND".$attribute); 
     $conditionSql = $this->_getConditionSql($attribute, $condition); 
    } 

} else { 
... 



最后我可以让我收集的位置过滤:

$category = Mage::getModel('catalog/category')->load($categoryId); 

$products = Mage::getModel('catalog/product') 
      ->getCollection() 
      ->addCategoryFilter($category) 
      ->addAttributeToFilter('position', array('gt'=>'0')) 
      ->setOrder('position', 'ASC') 
      ->load(); 



注:
显然,这是更好地覆盖以前的核心文件,而不是直接修改它们。

1

你必须使用

->addFieldToFilter('position', 'notnull') 

欲了解更多信息,看看there

+0

它不正常工作。 :(我已经知道这个链接,但是感谢分享:) – 2012-04-04 15:00:33

+0

你能告诉我们你的集合执行的查询吗? – haltabush 2012-04-04 15:24:18

+0

请在我的问题中看到'已编辑'部分,谢谢。 – 2012-04-04 15:42:56

1

我有一个类似的问题,并通过添加解决:

$products->getSelect() 
      ->joinLeft(array('category_product_index'=>Mage::getResourceModel('catalog/product_collection')->getTable('catalog/category_product_index')), '`e`.`entity_id` = `category_product_index`.`product_id`', array('position AS cat_index_position')) 
      ->group('e.entity_id'); 
相关问题