2013-02-26 62 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