我为客户实体创建了一个名为invoice_email的自定义属性。我现在要做的是获取所有客户,其中尚未填充invoice_email。我尝试了什么:
Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('invoice_email', array('eq' => ''))
这不起作用。我的数据库中有超过一千个客户,其中包含一个空的invoice_email字段,因此产生的0个条目是错误的。如果我以另一种方式尝试,比如
Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('invoice_email', array('neq' => ''))
它工作正常并返回6个条目。当我输出查询时,它看起来像这样:
SELECT `e`.*, `at_invoice_email`.`value` AS `invoice_email` FROM `customer_entity` AS `e` INNER JOIN `customer_entity_varchar` AS `at_invoice_email` ON (`at_invoice_email`.`entity_id` = `e`.`entity_id`) AND (`at_invoice_email`.`attribute_id` = '292') WHERE (`e`.`entity_type_id` = '1') AND (at_invoice_email.value = '')
所以问题似乎是INNER JOIN,如果我将它改为LEFT JOIN并且值=''值为IS NULL它的工作原理。有办法解决这个问题吗?
谢谢!
答案 0 :(得分:1)
您需要添加此属性以选择并将连接类型设置为左侧
$collection = Mage::getResourceModel('customer/customer_collection');
$collection->addAttributeToSelect('invoice_email', 'left');
$collection->addAttributeToFilter('invoice_email', array('null' => true));
答案 1 :(得分:0)
你可以尝试:
Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('invoice_email', array('null' => true));
答案 2 :(得分:0)
证明有一个选项可以强制加入一个jeft:
->addAttributeToFilter('invoice_email', array('null' => true), 'left')
重要的部分是最后一个参数,“左”。有了它就行了!