Question

I'm having trouble with using an OR filter in a Magento query. This is what i used:

 $collection = Mage::getModel('sales/order')->getCollection()
         ->addAttributeToSelect('*')
         ->addAttributeToFilter(
                            array(
                               array('attribute'=>'status','eq'=>'pending'), 
                               array('attribute'=>'created_at', 'from'=>$startDate, 'to'=>$finishDate)
                            )
     );

I want the following WHERE statement: WHERE 'status' = 'pending' OR (created_at < $startDate AND created_at > $finishDate) but i get the following error message

 Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Cannot determine the field name.' in /home/content/r/o/n/ronakkaria/html/magento/app/Mage.php:563 
 Stack trace: 
 #0 /home/content/r/o/n/ronakkaria/html/magento/app/code/core/Mage/Sales/Model/Resource/Collection/Abstract.php(52): Mage::throwException('Cannot determin...') 
 #1 /home/content/r/o/n/ronakkaria/html/magento/app/code/core/Mage/Sales/Model/Resource/Collection/Abstract.php(80): Mage_Sales_Model_Resource_Collection_Abstract->_attributeToField(Array) 
 #2 /home/content/r/o/n/ronakkaria/html/new/admin/magentoInvoice/getInvocieList.php(43): Mage_Sales_Model_Resource_Collection_Abstract->addAttributeToFilter(Array) 
 #3 {main} thrown in /home/content/r/o/n/ronakkaria/html/magento/app/Mage.php on line 563

I am currently using version 1.6-2rc.

Was it helpful?

Solution

Afaik you cannot use addAttributeToFilter() to OR two attributes for the sales/order model.

Use addAttributeToSearchFilter() instead:

$startDate = '2011-01-01';
$finishDate = '2011-01-31';

var_dump(
    Mage::getModel('sales/order')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToSearchFilter(
       array(
            array(
                'attribute' => 'status',
                'eq' => 'pending'
            ),
            array(
                'attribute' => 'created_at',
                'from' => $startDate,
                'to' => $finishDate
            )
       )
    )
    ->getSelectSql(true)
);

This will create a WHERE clause of:

WHERE 
    (status = 'pending') OR 
    (created_at >= '2011-01-01' AND created_at <= '2011-01-31')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top