2010-08-10 62 views
2

我想從Magento誰誰創造前一天誰誰誰更新前一天返回客戶。我試圖玩addFieldToFilter,沒有任何成功。複雜的查詢意味着日期

我也嘗試過操縱Zend_Db_Select,沒有成功。

所以現在我卡住了!

這裏有一些我的嘗試:

$customer = Mage::getModel('customer/customer'); 
$customers = $customer 
    ->getCollection() 
    ->getSelect() 
    ->where("updated_at >= ? AND updated_at <= ?",$this->getFrom(), $this->getTo()) 
    ->orWhere("e.created_at >= ? AND e.created_at <= ?", $this->getFrom(), $this->getTo()); 

或者

->addFieldToFilter(
    array(
     array('attribute'=>'updated_at', 'gteq'=>$this->getFrom()), 
     array('attribute'=>'created_at', 'gteq'=>$this->getFrom()) 
    ), 
    '', 
    'left' 
); 

感謝

+0

什麼是生成的SQL查詢看起來像? (var_dump((string)$ customer-> getSelect());) – 2010-08-10 18:49:40

回答

2

我建議不要直接操縱的選擇,除非它是絕對必要的,你知道到底發生了什麼幕後在你的版本Magento的。

以下語法應該處理棘手的部分你

$c = Mage::getModel('customer/customer') 
->getCollection() 
->addAttributeToFilter(array(    
    array('attribute'=>'updated_at','from'=>'2010-05-12','to'=>'2010-05-30'), 
    array('attribute'=>'created_at','from'=>'2010-05-12','to'=>'2010-05-13') 
)); 

var_dump((string) $c->getSelect()); 
var_dump(count($c)); 

所有你需要做的是下降的日期範圍你想要的。

+0

從技術上講,這正是我所問的,謝謝!但是silvo的竅門也是有用的:我可以忽略created_at,因爲updated_at是在創建用戶時自動設置的。 – frinux 2010-08-11 07:29:51

2

這就夠用的updated_at作爲過濾器屬性,因爲它被設置爲當前創建用戶時的日期時間。因此,通過篩選此字段,您將獲得新用戶以及那些不是新的用戶,但在給定時間段內已更新的用戶。下面的代碼來尋找用戶在過去24小時內更新或創建:

$customers = Mage::getModel('customer/customer')->getCollection(); 
$customers->addAttributeToFilter('updated_at', array('gt' => date("Y-m-d H:i:s", time()-60*60*24))); 

foreach($customers as $customer) { 
    //do sth 
} 
0

感謝艾倫和林牧複合,這裏是我寫的:

->addAttributeToFilter(array(    
    array('attribute'=>'updated_at','from'=>$this->getFrom(),'to'=>$this->getTo()) 
)); 

這兩個答案都是有用的。謝謝!