1

我在 Magento 查询中使用 OR 过滤器时遇到问题。这是我用的:

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

我想要以下 WHERE 语句:WHERE 'status' = 'pending' OR (created_at < $startDate AND created_at > $finishDate) 但我收到以下错误消息

 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

我目前使用的是 1.6-2rc 版本。

4

1 回答 1

2

Afaik 你不能使用模型addAttributeToFilter()OR两个属性sales/order

改用addAttributeToSearchFilter()

$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)
);

这将创建一个 WHERE 子句:

WHERE 
    (status = 'pending') OR 
    (created_at >= '2011-01-01' AND created_at <= '2011-01-31')
于 2011-09-26T13:22:14.110 回答