Magento在where子句中使用运算符

时间:2014-10-13 16:26:09

标签: database magento zend-framework

我的产品具有date_createdexpires_in属性。 date_created是Magento日期后端格式,而expires_in是包含1周,1个月等选项的选项。

使用这两个属性我正在尝试确定过期产品的总数。我的想法是编写一个选择:

的查询
  1. 产生日期+ 1周的产品< now()AND expires_in = 1周 AND
  2. 创建日期+ 1个月的产品< now()AND expires_in = 1个月
  3. ...
  4. 但我似乎无法让第一步的第一部分工作:

    $currentTimestamp = Mage::getModel('core/date')->timestamp(time());
    $oneWeekTimestamp = Mage::getModel('core/date')->timestamp(strtotime("+1 week"));
    
    $products = Mage::getResourceModel('catalog/product_collection')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->addAttributeToSelect('')
    ...
    //these are what i have tried so far:
    $products->addAttributeToFilter('date_updated + ' . $oneWeekTimestamp , array('gt' => $currentTimestamp));
    $products->addAttributeToFilter(new Zend_Db_Expr('date_updated + ' . $oneWeekTimestamp), array('gt' => $currentTimestamp));
    $products->addAttributeToFilter("DATEDIFF($currentTimestamp, 'date_updated')" , array('gt' => 7));
    
    $countExpired = $products -> count();
    

    它们似乎都不起作用。如果可能的话,我会喜欢交叉RDBMS解决方案。

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式执行此操作:

 $currentTimestamp = Mage::getModel('core/date')->timestamp(time());
 $oneWeekTimestamp = Mage::getModel('core/date')->timestamp(strtotime("+1 week"));

 $oneweekDate=date("Y-m-d H:i:s",$oneWeekTimestamp);

 $products->addAttributeToFilter('date_updated',array('date'=>true,'gt',$oneweekDate);