Note that there are some explanatory texts on larger screens.

plurals
  1. POMagento: Invalid Attribute Name error
    primarykey
    data
    text
    <p>I'm trying to get all the products stored under a certain category.<br> <br> Everything is working fine writing:</p> <pre><code>$category = Mage::getModel('catalog/category')-&gt;load($categoryId); $products = Mage::getModel('catalog/product') -&gt;getCollection() -&gt;addCategoryFilter($category) -&gt;setOrder('position', 'DESC') -&gt;load(); </code></pre> <p>I've got all the products correctly sorted by their position attribute.<br> Since some of them don't have any position set, I want to exclude them, so I tried simply:</p> <pre><code>$category = Mage::getModel('catalog/category')-&gt;load($categoryId); $products = Mage::getModel('catalog/product') -&gt;getCollection() -&gt;addCategoryFilter($category) -&gt;addFieldToFilter( 'position', array('gt'=&gt;0) ) -&gt;setOrder('position', 'DESC') -&gt;load(); </code></pre> <p>But the page can't be loaded, I get an error 'There has been an error processing your request' and an error log is stored under var/report starting with</p> <blockquote> <p>a:5:{i:0;s:33:"Invalid attribute name: position.</p> </blockquote> <p>And I cannot figure out to solve this .. I googled a lot and tried a lot of solutions, but no one worked.<br> I cannot understand why it accepts an attribut for sorting while not for filtering. <br>Any idea? <br> <br> EDITED:<br> The query performed by $products is</p> <pre><code>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 </code></pre> <p><br> I tried also:</p> <pre><code>$category = Mage::getModel('catalog/category')-&gt;load($categoryId); $products = Mage::getModel('catalog/product') -&gt;getCollection() -&gt;addCategoryFilter($category) -&gt;addFieldToFilter( 'cat_index_position', 'notnull' ) -&gt;setOrder('position', 'ASC') -&gt;load(); </code></pre> <p>with same result:</p> <pre><code>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...') </code></pre> <p><br> <br> <strong>EDITED:</strong><br> I think my problem is similar to <a href="https://stackoverflow.com/questions/3434971/after-join-cannot-filter-by-attribute-qty-getting-products-from-inventory-tha">this one</a> but I cannot figure out how to solve it!</p> <p><br> <br> <strong>EDITED:</strong><br></p> <p>Tried also</p> <pre><code>$category = Mage::getModel('catalog/category')-&gt;load($categoryId); $products = Mage::getModel('catalog/product') -&gt;getCollection() -&gt;addAttributeToSelect('cat_index_position') -&gt;addAttributeToFilter('cat_index_position', array('gt'=&gt;'0')); </code></pre> <p>Always the same error</p> <p><br> <br> <strong>EDITED:</strong><br></p> <p>If I add</p> <pre><code>WHERE `position` &gt; 0 </code></pre> <p>In my query browser it's working!<br> So how can I add it to my query within magento?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload