Magento:如何在客户集合中以编程方式搜索

问题描述:

我正在尝试构建搜索脚本以从客户集合中检索条目。诀窍是正常的“或”条件仅适用于第一个条目。Magento:如何在客户集合中以编程方式搜索

这是我到目前为止的代码:

$customers = Mage::getResourceModel('customer/customer_collection') 
     ->addAttributeToSelect('*'); 

    if($_searchTerm = $this->getRequest()->getParam('q')){ 
     $customers->addAttributeToFilter(
      array(
       array('attribute' => 'firstname', 'like' => $_searchTerm), 
       array('attribute' => 'lastname', 'like' => $_searchTerm), 
       array('attribute' => 'email', 'like' => '%' . $_searchTerm . '%'), 
       array('attribute' => 'phone', 'like' => '%' . $_searchTerm . '%'), /* valid field in my collection*/ 
      ) 
     ); 
    } 

我试过用通配符“%”,但也仍然没有正确的结果。 我很可能在这里错过了一些东西。

谢谢。

+1

它看起来对我很好。当我测试上述结果查询结束WHERE ...((at_firstname.value LIKE'TEST')或(at_lastname.value LIKE'TEST')或(e.email LIKE'%TEST%')或(at_phone .value LIKE'%TEST%'))'这是预期的。究竟发生了什么问题? – clockworkgeek 2014-09-19 11:01:42

+0

@clockworkgeek我已经使用 - > getSelect()方法来显示“选择”SQL查询,一切看起来很正常。我有2个客户:Ion和Daniel。当我使用脚本时,它的行为就像他只在“Ion”客户中搜索而不是“Daniel”。所有结果都显示给“Ion”客户。 – aki 2014-09-19 11:05:48

这次我的愚蠢超过了我。我在查询中使用了另一个属性,Magento在过滤时遇到了一些问题,这是使用模块创建的自定义属性。感谢@clockworkgeek支持我。