1

I am new to using magento collections and am receiving exhausted memory fatal errors. I just want to double check that my collection is pulling what i think it is.

I believe this is grabbing all products including the attribute 'part_num'. It then filters out all products with part_num == '' (blank) or ' ' (space).

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

$collection->addAttributeToSelect('part_num'); 

$collection->addAttributeToFilter(array(
array('attribute'=>'part_num','neg'=>''),
));
$collection->addAttributeToFilter(array(
array('attribute'=>'part_num','neg'=>' '),
));

This would leave me with all products that have some data in the part_num attribute. Is this correct?

  • 1
    Use Mage::log((string) $collection->getSelect()); Then view the log and see that SQL you're getting. Try the SQL in your client and it may help you see if there is a huge payload coming back - whether it is more than you want. – Qoheleth-Tech May 20 '15 at 23:59
  • Thanks that is a nifty trick! Its exactly what I wanted. – beepboop May 21 '15 at 0:11
2

This looks about right, however your filter is wrong. neg should be neq.

So:

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

$collection->addAttributeToSelect('part_num'); 

$collection->addAttributeToFilter('part_num',array('neq'=>''));
$collection->addAttributeToFilter('part_num',array('neq'=>' '));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.