1

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
rosh3000
  • 309
  • 2
  • 16

1 Answers1

2

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')
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
  • Btw, the condition you mention below the first code part of your question makes no sense (at least to me). Since it means "include orders which were created both, *before* `$startDate` AND *after* `$finishDate` at the same time", which most likely never will happen (except you're going to use weird values^^). I assume this was just a typo, right? – Jürgen Thelen Sep 26 '11 at 13:22