0
votes

I've added a custom filed to the customer addresses, but I don't know how to query using this field.

I'm executing the following piece of code:

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

But I'm facing this error:

Fatal error: Call to a member function getBackend() on a non-object in app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816

My question is: how can I query through this field?

Thanks.

2

2 Answers

0
votes

After a few tests, I could solve the problem. Here's the code:

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

Try using

Mage::getResourceModel('customer/address_collection')

Instead of

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

See /app/code/core/Mage/Customer/Model/Customer.php

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