2012-11-28 59 views
3

我在使用自定義表格收集日期過濾器時遇到困難。已經搜索herehere和其他一些地方,但仍然無法得到我所需要的。問題是我無法將NULL值添加到結果集中。Magento,按日期期限過濾收集幷包含空值

到目前爲止,經過多次試驗和錯誤的測試我當前的代碼:

$myBannersCollection = Mage::getModel('banners/bannersadmin')->getCollection() 
        ->addfieldtofilter('banner_prod_id',$currentProdID) 
        ->addfieldtofilter('banner_start_date', 
         array(
         array('from' => Mage::getModel('core/date')->gmtDate()), 
           'banner_start_date' => null)) 
        ->addfieldtofilter('banner_end_date', 
         array(
         array('gteq' => Mage::getModel('core/date')->gmtDate()), 
           'null' => true) 
           ); 
var_dump((string) $myBannersCollection->getselect()); 

此代碼。OUPUTS下面的SQL代碼片段:

SELECT `main_table`.* 
    FROM `dts_banners_admin` AS `main_table` 
WHERE (banner_prod_id = '16') 
    AND (((banner_start_date >= '2012-11-28 14:39:13') OR (banner_start_date=''))) 
    AND (banner_end_date IS NULL) 

嘗試了好幾種不同的選項來添加NULL條件,但沒有我可以得到像這樣的東西:

SELECT `main_table`.* 
    FROM `dts_banners_admin` AS `main_table` 
WHERE (banner_prod_id = '16') 
    AND (((banner_start_date>='2012-11-28 14:39:13') OR (banner_start_date IS NULL))) 
    AND ((banner_end_date >= '2012-11-28 14:39:13') OR (banner_end_date IS NULL)) 

PS: Magento在addfieldtofilter上有BETWEEN運營商嗎?

回答

11

Got it!感謝這個SO answer。需要爲IS NULL子句添加另一個數組。現在的代碼是:

$myBannersCollection = Mage::getModel('banners/bannersadmin')->getCollection() 
        ->addfieldtofilter('banner_prod_id',$currentProdID) 
        ->addfieldtofilter('banner_start_date', 
         array(
         array('to' => Mage::getModel('core/date')->gmtDate()), 
           array('banner_start_date', 'null'=>''))) 
        ->addfieldtofilter('banner_end_date', 
         array(
         array('gteq' => Mage::getModel('core/date')->gmtDate()), 
          array('banner_end_date', 'null'=>'')) 
           ); 

並將其輸出:

SELECT `main_table`.* 
    FROM `dts_banners_admin` AS `main_table` 
WHERE (banner_prod_id = '16') 
AND (((banner_start_date>='2012-11-28 15:12:03') OR (banner_start_date IS NULL))) 
    AND (((banner_end_date >= '2012-11-28 15:12:03') OR (banner_end_date IS NULL))) 

EDIT

從代替改性的banner_start_date如我用等的期間是錯誤地解決了,沒有數據被返回。

+0

**至** <=> ** <=** not **> = ** – HoangHieu