4

我有一个名为 rms_id 的自定义属性。它是与我们在另一个数据库中的客户相关联的唯一帐号。我需要一种通过此 ID 查找客户的方法,而无需手动访问数据库。

截至目前,我可以通过电子邮件地址加载客户,如下所示:

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

这很好,但是电子邮件并不总是可用。RMS id 是。有没有办法通过自定义属性加载用户?

理论上,以下方法会起作用:

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

但是它出错了。任何帮助将非常感激。

4

4 回答 4

18

核心中有一个很好的例子;见Mage_Catalog_Model_Abstract::loadByAttribute()[链接]。它涉及使用数据模型来检索集合、将属性加入其中并按该属性进行过滤。如果属性不是静态的,即不是实体表的一部分,这是必要的。

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

if (is_object($result)) {
    /* Logic */
}
于 2012-12-18T15:19:35.143 回答
1

但在实践中,你必须想出自己的方法,loadByEmail()类似于app/code/core/Mage/Customer/Model/Resource/Customer.php

或从收藏中获取电子邮件

Mage::getModel('customer/customer')->getCollection()->addFieldToFilter('rms_id', 'youremailhere')->load();
于 2012-12-18T15:25:44.880 回答
0
$customer->load($data['account_id'], 'rms_id');
于 2012-12-18T15:20:30.640 回答
0

你也可以这样做:

$customer = Mage::getModel('customer/customer') ->getCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('rms_id', $data['account_id']) ->getFirstItem(); 将返回Mage_Customer_Model_Customer对象。

于 2016-02-19T14:14:04.643 回答