【问题标题】:Error creating Magento API method - getAllowedAttributes创建 Magento API 方法时出错 - getAllowedAttributes
【发布时间】:2013-10-09 20:20:24
【问题描述】:

我正在创建一个向magento API sopa_v1添加方法的模块:我的“etc”中的api.xml如下:

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <obterenderecosfiltrado_api translate="title" module="obterenderecosfiltrado">
                <title>Myapi</title>
                <acl>obterenderecosfiltrado/api</acl>
                <model>obterenderecosfiltrado/api</model>
                <methods>                    
                        <getaddressbyfilter translate="title" module="obterenderecosfiltrado">
                            <title>Obter Address Filtrado</title>
                            <acl>obterenderecosfiltrado/getaddressbyfilter</acl>
                        </getaddressbyfilter>
                </methods>
            </obterenderecosfiltrado_api>
        </resources>
        <acl>
            <resources>
                <obterenderecosfiltrado translate="title" module="obterenderecosfiltrado">
                    <title>ObterEnderecosFiltrado</title>
                    <sort_order>2000</sort_order>                    
                    <getaddressbyfilter translate="title" module="obterenderecosfiltrado">
                        <title>Obter Address Filtrado</title>
                    </getaddressbyfilter>
                </obterenderecosfiltrado>
            </resources>
        </acl>
    </api>
</config>

和我在“模型”中的 Api.php:

<?php
class Novapc_ObterEnderecosFiltrado_Model_Api extends Mage_Api_Model_Resource_Abstract
{        

        protected $_mapAttributes = array(
            'customer_address_id' => 'entity_id'
        );

        public function __construct()
        {
            $this->_ignoredAttributeCodes[] = 'parent_id';
        }

        public function getaddressbyfilter($filters)
        {
        $collection = Mage::getModel('customer/address')->getCollection()
            ->addAttributeToSelect('*');

        if (is_array($filters)) {
            try {
                foreach ($filters as $field => $value) {
                    if (isset($this->_mapAttributes[$field])) {
                        $field = $this->_mapAttributes[$field];
                    }

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

        $result = array();
        foreach ($collection as $address) {
            $data = $address->toArray();
            $row  = array();

            foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
                $row[$attributeAlias] = (isset($data[$attributeCode]) ? $data[$attributeCode] : null);
            }

            foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
                if (isset($data[$attributeCode])) {
                    $row[$attributeCode] = $data[$attributeCode];
                }
            }

            $customerId = $address->getParentId();

            $customer = Mage::getModel('customer/customer')->load($customerId);

            $row['customer_id'] = $customerId;
            $row['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
            $row['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();

            $result[] = $row;
        }

        return $result;
        }
}

O Erro esta na TAG:

  foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
     if (isset($data[$attributeCode])) {
        $row[$attributeCode] = $data[$attributeCode];
     }
  }

retorna o seguinte:

Uncaught SoapFault exception: [SOAP-ENV:Server] Call to undefined method Novapc_ObterEnderecosFiltrado_Model_Api::getAllowedAttributes() in C:\wamp\www\conectar_ws.php:13 Stack trace: #0 C:\wamp\www\conectar_ws.php(13): SoapClient->__call('call', Array) #1 C:\wamp\www\conectar_ws.php(13): SoapClient->call('739882c5e4d2184...', 'obterenderecosf...', Array) #2 {main} thrown in C:\wamp\www\conectar_ws.php on line 13

【问题讨论】:

  • 我觉得很清楚。您的类 Novapc_ObterEnderecosFiltrado_Model_Api 和 Mage_Api_Model_Resource_Abstract(它是从它扩展而来)都没有方法 getAllowedAttributes()。创建它并且应该可以工作。

标签: php web-services api magento


【解决方案1】:

基本上,这个错误意味着在 Novapc_ObterEnderecosFiltrado_Model_Api 和它扩展的类中都没有方法 getAllowedAttributes - Mage_Api_Model_Resource_Abstract

所以你需要在你的类中实现这个方法

【讨论】:

  • 能告诉我如何在我的课堂上实现这个方法吗?我从另一个程序员那里得到这段代码,我有点迷茫。
  • 对不起,我设法解决了,我刚刚将我的课程扩展到“Mage_Customer_Model_Address_Api”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 2013-10-04
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多