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?

2
  • 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.
    – user14762
    May 20, 2015 at 23:59
  • Thanks that is a nifty trick! Its exactly what I wanted.
    – beepboop
    May 21, 2015 at 0:11

1 Answer 1

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'=>' '));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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