复杂查询暗示日期

时间:2010-08-10 16:18:16

标签: php orm magento zend-db

我想让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'
);

由于

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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));

您需要做的只是放入您想要的日期范围。

答案 2 :(得分:0)

感谢Alan和Silvo,这是我写的:

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

这两个答案都很有用。谢谢!

相关问题