4
votes

I have a custom attribute called rms_id. It's a unique account number that's associated with our customers in another database. I need a way to lookup the customer by this id, without hitting the database manually.

As of now, I can load the customer by email address like so:

$customer->loadByEmail($data['email']);

This is great, however the email is not always available. The RMS id is. Is there any way to load a user by the custom attribute?

In theory, the following would work:

$customer->loadByRmsId($data['account_id']);

However it errors out. Any help would be much appreciated.

4

4 Answers

18
votes

There is a good example of this in the core; see Mage_Catalog_Model_Abstract::loadByAttribute()[link]. It involves using a data model to retrieve a collection, joining the attribute in, and filtering by that attribute. This is necessary if the attribute is not static i.e. not a part of the entity table.

$result = Mage::getModel('customer/customer')
              ->getCollection()
              ->addAttributeToSelect('rms_‌​id')
              ->addAttributeToFilter('rms_id',{Val})->load();

if (is_object($result)) {
    /* Logic */
}
1
votes

but in practice you have to come up with your own method similar to loadByEmail() in app/code/core/Mage/Customer/Model/Resource/Customer.php

or get the e-mail from collection

Mage::getModel('customer/customer')->getCollection()->addFieldToFilter('rms_id', 'youremailhere')->load();
0
votes
$customer->load($data['account_id'], 'rms_id');
0
votes

You can do it also like that:

$customer = Mage::getModel('customer/customer') ->getCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('rms_id', $data['account_id']) ->getFirstItem(); Will return Mage_Customer_Model_Customer object.