2013-02-26 77 views
0

我正在创建一个magento扩展,我需要搜索基于客户的名字。我知道我可以在sql的帮助下做到这一点。但我想用magento的客户模型来完成这项工作。Magento按名搜索客户

我已经使用了这个,它返回一个所有用户的数组,但我想要所有用户,而不是所有用户都符合条件的特定用户。

$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*'); 

回答

3

你必须使用addAttributeToFilter方法,使用像一个简单的WHERE where子句中可以使用通配符,太。

<?php 
include 'app/Mage.php'; 
Mage::app(); 

$query = "John"; 

$model = Mage::getSingleton('customer/customer'); 

$result = $model->getCollection() 
     ->addAttributeToSelect('*') 
     ->addAttributeToFilter('firstname', array('like' => "%$query%")); 

foreach($result as $r) 
{  
     $customer = $model->load($r->getId()); 
     echo '<b>'.$customer->getFirstname().' '.$customer->getLastname().'</b><br/>'; 
} 
+0

嘿,你能告诉我把磁盘放在哪里吗?以及如何搜索表单将对此文件执行操作? – Xabby 2016-08-31 10:34:24

1

$c = Mage::getModel('customer/customer')->getCollection()->addAttributeToFilter('first_name', array('like' => $the_name_you_want_to_find))

+0

谢谢,但有些不正确。 – 2013-02-26 07:24:27

+0

也许我不知道你称为你的属性,如果它的“名字”或“名字”,如果你不喜欢“喜欢” - 查询你可以使用eq代替。哦,当然在getCollection()之后没有中断 - – Soundz 2013-02-26 12:48:07