0

次のコードを作成しました。

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
$dateTomorrow = date('m/d/y', $tomorrow);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('special_price', array('neq' => ""))
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(0 => array('date' => true, 'from' => $dateTomorrow), 1 => array('is' => new Zend_Db_Expr('null')))), 'left');

これはうまくいっています。しかし、私が望むのは、今すぐ OR ステートメントを使用することです。私はこれでそれを修正できると思った:

$collection->addAttributeToFilter(array(
array('attribute'=> 'someattribute','like' => 'value'),
array('attribute'=> 'otherattribute','like' => 'value'),
array('attribute'=> 'anotherattribute','like' => 'value'),
));

しかし、成功はありません。したがって、私の質問は、これに OR ステートメントを追加する方法です。

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
$dateTomorrow = date('m/d/y', $tomorrow);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('special_price', array('neq' => ""))
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(0 => array('date' => true, 'from' => $dateTomorrow), 1 => array('is' => new Zend_Db_Expr('null')))), 'left');

また

ありがとうございました。

よかったね、レックス

4

2 に答える 2

3

マジェントウィキから:

// Add OR condition:
$collection->addAttributeToFilter(array(
    array(
        'attribute' => 'field_name',
        'in'        => array(1, 2, 3),
        ),
    array(
        'attribute' => 'date_field',
        'from'      => '2000-09-10',
        ),
    ));

だから、私が理解しているように、あなたのクエリを書くには

"....WHERE special_price <> "" AND special_from_date <= '2012-07-02' AND (special_to_date >= '2012-07-03' OR special_to_date IS NULL)..." 

以下を使用します。

$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('special_price', array('neq' => ""))
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter(array(
    array(
        'attribute' => 'special_to_date',
        'date' => true,
        'from' => $dateTomorrow
    ),
    array(
        'attribute' => 'special_to_date',
        'null' => 1
    )
));
于 2012-07-02T16:10:52.737 に答える
2

以下のリンクを確認してください。

于 2012-07-02T15:41:30.103 に答える