Magento API,返回带有NULL值的订单

时间:2012-02-13 19:41:52

标签: api magento

使用magento api版本1和肥皂。

需要使用'coupon_code'=>返回所有订单NULL

我正在尝试的电话:

$order_listAR = $proxy->call($sessionId, 'sales_order.list', array(array('coupon_code'=>array('null'=>'null'))));

我想要的输出是:

array(237) {
    ["state"]=>
    string(8) "complete"
    ["status"]=>
    string(8) "complete"
    ["coupon_code"]=> NULL

到目前为止,这似乎工作正常,但我不确定('null'=>'null')是否是在数组中查找NULL值的正确方法。有人可以解释为什么这样可行,或者,或者这是否是正确的语法?我没有任何错误的余地。

1 个答案:

答案 0 :(得分:4)

是的,您使用的语法对null进行过滤是正确的。

array(
    'coupon_code' => array(
        'null' => 'this_value_doesnt_matter'
    ) 
)

Magento将 * API方法sales_order.list映射到Mage_Sales_Model_Order_Api::items()

public function items($filters = null)
{
    :
    $collection = Mage::getModel("sales/order")->getCollection()
    :
    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_attributesMap['order'][$field])) {
                    $field = $this->_attributesMap['order'][$field];
                }

                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    :
}

items()方法使用Mage_Sales_Model_Resource_Order_Collection来获取API调用的订单。该集合基于Varien_Data_Collection_Db,所以

  • $collection->addFieldToFilter($field, $value)

从上面基本上调用

  • Varien_Data_Collection_Db::addFieldToFilter()

如果你遵循后者,你最后会点击Varien_Db_Adapter_Pdo_Mysql::prepareSqlCondition(),而

是参数
  • $fieldName = 'coupon_code'
  • $condition = array('null' => 'null')

该方法的摘录:

public function prepareSqlCondition($fieldName, $condition)
{
    $conditionKeyMap = array(
        'eq'            => "{{fieldName}} = ?",
        :
        'notnull'       => "{{fieldName}} IS NOT NULL",
        'null'          => "{{fieldName}} IS NULL",
        : 
        'sneq'          => null
    );
    :

    $query = '';
    if (is_array($condition)) {
        :
        $key = key(array_intersect_key($condition, $conditionKeyMap));

        if (isset($condition['from']) || isset($condition['to'])) {
            :
        } elseif (array_key_exists($key, $conditionKeyMap)) {
            $value = $condition[$key];
            if (($key == 'seq') || ($key == 'sneq')) {
                :
            }
            $query = $this->_prepareQuotedSqlCondition($conditionKeyMap[$key], $value, $fieldName);
        } else {
            :
        }
    }
    :
}

在您的情况下,将使用

调用_prepareQuotedSqlCondition()
  • $text = '{{fieldName}} IS NULL'
  • $value = 'null'
  • $fieldName = 'coupon_code'

将导致$query = 'coupon_code IS NULL'

如果您仔细查看转换方法

protected function _prepareQuotedSqlCondition($text, $value, $fieldName)
{
    $sql = $this->quoteInto($text, $value);
    $sql = str_replace('{{fieldName}}', $fieldName, $sql);
    return $sql;
}

您还会看到,为什么'null' => 'null'键/值对的根本不重要。这是因为$text将是'{{fieldName}} IS NULL',即不包含任何绑定?

因此,_quoteInto() ^^

无需替换

*请参阅app/code/core/Mage/Sales/etc/api.xml

相关问题