【问题标题】:Magento resource: custom selects with OR in where (select orders by status)Magento 资源:在 where 中使用 OR 进行自定义选择(按状态选择订单)
【发布时间】:2014-04-02 16:40:57
【问题描述】:

我正在构建一个自定义模块,我正在尝试获取具有特定状态的所有订单:

$canceledQuery = Mage::getSingleton('core/resource')->getConnection('core_read')->select()
            ->from('mage_sales_flat_order', 'entity_id')
            ->where('status = ?', 'canceled');

这很好用。但现在我正在尝试在 where 中使用 OR,但没有成功。 我一直在尝试这个:

$whereCanceled = array();
foreach ($_status as $statusCode => $value) {
    $whereCanceled[] = sprintf('%s=:%s', 'status', $statusCode);
}

$ordersQuery = Mage::getSingleton('core/resource')->getConnection('core_read')->select()
            ->from('mage_sales_flat_order', 'entity_id')
            ->where(implode(' OR ', $whereCanceled));

所以,我不知道在这种情况下如何正确使用 OR。我发现这对 OR 没有用。有什么想法吗?

【问题讨论】:

  • 你的代码有什么问题?
  • 不起作用。我尝试了许多查询,其中一种状态成功,然后我使用 implode 来获取所有结果,只是返回空。

标签: sql magento collections orders


【解决方案1】:

使用 join 代替 implode。 magento 使用 join 本身。

->where(join(' OR ', $orWhere));//$orWhere array of condition

您可以在下面的函数 magento 中看到 join 用于 where 子句中的 OR 条件

public function getSystemConfigByPathsAndTemplateId($paths, $templateId)
    {
        $orWhere = array();
        $pathesCounter = 1;
        $bind = array();
        foreach ($paths as $path) {
            $pathAlias = 'path_' . $pathesCounter;
            $orWhere[] = 'path = :' . $pathAlias;
            $bind[$pathAlias] = $path;
            $pathesCounter++;
        }
        $bind['template_id'] = $templateId;
        $select = $this->_getReadAdapter()->select()
            ->from($this->getTable('core/config_data'), array('scope', 'scope_id', 'path'))
            ->where('value LIKE :template_id')
            ->where(join(' OR ', $orWhere));

        return $this->_getReadAdapter()->fetchAll($select, $bind);
    }

更多参考打开文件位于 [magento]/app/code/core/Mage/Core/Model/Resource/Email/Template.php

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 2021-04-07
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多