我如何使用集合以Magento方式运行该SQL?

时间:2014-05-21 12:53:08

标签: magento collections

这是我的SQL:

select created_at from sales_flat_order where created_at > SUBDATE(CURRENT_TIMESTAMP, INTERVAL 3 HOUR); 

这是我最近3小时的订单。现在,我将如何使用Magento集合运行该查询?

这是我到目前为止所做的:

$orderCollection = Mage::getModel('sales/order')->getCollection();

现在我需要添加一些过滤器,但我被困在那里。谢谢你的帮助!

2 个答案:

答案 0 :(得分:3)

看看@ Using Collections in Magento

$orderCollection = Mage::getModel('sales/order')->getCollection();
$orderCollection->addAttributeToFilter('created_at', array(
    'gt' => new Zend_Db_Expr('SUBDATE(CURRENT_TIMESTAMP, INTERVAL 3 HOUR)'))

$orderCollection = Mage::getModel('sales/order')->getCollection();
$orderCollection->getSelect()->where('created_at > SUBDATE(CURRENT_TIMESTAMP, INTERVAL 3 HOUR)');

答案 1 :(得分:2)

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 10800; // 3hr(60min/hr)(60sec/min)
$from = date('Y-m-d H:i:s', $lastTime);
$order =Mage::getModel('sales/order')->getCollection();
    ->addAttributeToSelect('*')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
    ->load();

如果您有任何疑问,请告诉我