Magento:使用自定义字段查询客户地址

时间:2013-12-26 21:50:30

标签: magento magento-1.7

我已在客户地址中添加了自定义文件,但我不知道如何使用此字段进行查询。

我正在执行以下代码:

  

$ collection = Mage :: getModel('customer / address')        - > getCollection()        - > addAttributeToSelect( 'custom_field')        - > addFieldToFilter('custom_field','1');   echo var_dump($ collection);

但是我遇到了这个错误:

  

致命错误:在非对象中调用成员函数getBackend()   第816行的app \ code \ core \ Mage \ Eav \ Model \ Entity \ Abstract.php

我的问题是:如何通过此字段查询?

感谢。

2 个答案:

答案 0 :(得分:0)

尝试使用

  

法师:: getResourceModel( '顾客/ address_collection')

而不是

  

Mage :: getModel('customer / address') - > getCollection()

请参阅/app/code/core/Mage/Customer/Model/Customer.php

$collection = Mage::getResourceModel('customer/address_collection')
                   ->addAttributeToSelect('custom_field') 
                   ->addAttributeToFilter('custom_field', '1')
                   ->getItems();

答案 1 :(得分:0)

经过几次测试后,我可以解决问题。这是代码:

$collection = Mage::getResourceModel('customer/address_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('custom_field', 1)
                ->getItems();
echo print_r($collection, true);