The Magento object for interacting with customers (Mage_Customer_Model_Customer
, alias customer/customer
) has three address fetching methods, but you probably only want one of them.
The first, and least useful, is getAddressCollection
. You might think this method would return a Magento Address collection containing address objects beloning to the customer represented by the object. You’d be half right, but the wrong half is horribly wrong.
public function getAddressCollection()
{
return Mage::getResourceModel('customer/address_collection');
}
This method returns an instantiated, but unfiltered collection object. That means it’ll contains every address in the system if you load it. This method should probably be protected
instead of public, as it’s only used internally by the Customer object to get a reference to the collection for filtering (see below). However, once made public the core team would have to leave it public or risk breaking someone’s code. When you’re providing system code (core team) for client programmers (module and theme developers) to use, once you make something public that means supporting it forever, or being ready to have the difficult conversations with your community about dropping support.
So, next up is the similarly named getAddressesCollection
. This method
public function getAddressesCollection()
{
if (is_null($this->_addressesCollection)) {
$this->_addressesCollection = $this->getAddressCollection()
->setCustomerFilter($this)
->addAttributeToSelect('*');
foreach ($this->_addressesCollection as $address) {
$address->setCustomer($this);
}
}
return $this->_addressesCollection;
}
instantiates a collection object, sets a customer filter on it, adds all attributes to the address select, and then assigns a customer object reference to the address object, tying it to a particular customer. This is pretty straight forward, and probably the method your’re after.
One thing to notice here is there’s nothing in the collection address loading logic that automatically assigns a customer object to the collection. Normally, this is handled individually on each address object in the getCustomer
method.
public function getCustomer()
{
if (!$this->getCustomerId()) {
return false;
}
if (empty($this->_customer)) {
$this->_customer = Mage::getModel('customer/customer')
->load($this->getCustomerId());
}
return $this->_customer;
}
However, the core team probably chose to assign it here as a performance optimization to avoid multiple individual load
s, especially for customers with large numbers of addresses.
Finally, there’s the getAddresses
method
public function getAddresses()
{
$this->_addresses = $this->getAddressesCollection()->getItems();
return $this->_addresses;
}
This method uses every collection’s built-in getItems
method to return a PHP array of address objects.
So, getAddressesCollection
is when you want to further manipulate the collection before loading it, getAddresses
is when you just want the addresses, and getAddressCollection
is for never.