首页 > 解决方案 > Magento 通过 eav 选项值查询客户

问题描述

我们为出生日期创建了自定义客户属性:

dob_month, dob_day

它们是 select 的前端输入类型和 int 的后端类型。我们试图从客户那里获得以下字段:entity_id、、dobstore_id(如果可能,一个dob字段,否则,分为dob_monthdob_day)。

这是我们正在苦苦挣扎的情况,我们想按这些选项值进行过滤。示例:dob_month = 05 和 dob_day = 08

我们尝试过的所有操作要么必须按存储在 customer_entity_int 中的值进行过滤,例如:1125 = eav_option_value of 05,要么我们只能检索存储在 customer_entity_int 中的值。

我想一定有办法。您能提供的任何帮助将不胜感激。

这是我得到的最接近的:

$collection = Mage::getResourceModel('customer/customer_collection')
                ->addAttributeToSelect('email')
                ->addAttributeToSelect('dob_month', 'left')
                ->addAttributeToSelect('dob_day', 'left')
                ->addExpressionAttributeToSelect('dob', "CONCAT({{dob_month}}, '-',{{dob_day}})", array(
                'dob_month', 'dob_day'
                ))
->addAttributeToFilter('dob_month', array(
    array('eq' => '1125')
))
/*->addAttributeToFilter('dob_day', array(
    array('eq' => '08')
))*/
->setPageSize(20)
->setCurPage(1);

注意:PageSize 和 CurPage 仅用于测试以限制结果。

标签: magento

解决方案


万一有人想要它,我最好的办法就是通过 SQL 来完成这一切:

SELECT e.entity_id,CONCAT(dob_month_eav.value, '-',dob_day.dob_day) as dob,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv 
ON ( cv.attribute_id = (
    SELECT attribute_id FROM eav_attribute 
    WHERE entity_type_id = e.entity_type_id 
    AND attribute_code = 'dob_month'
    )
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_month_eav` ON dob_month_eav.option_id=cv.value 
LEFT JOIN (SELECT e.entity_id,dob_day_eav.value as dob_day,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv 
ON ( cv.attribute_id = (
    SELECT attribute_id FROM eav_attribute 
    WHERE entity_type_id = e.entity_type_id 
    AND attribute_code = 'dob_day'
    )
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_day_eav` ON dob_day_eav.option_id=cv.value) as dob_day ON dob_day.entity_id=e.entity_id
WHERE dob_month_eav.store_id=0 AND dob_month_eav.value='05' AND dob_day='08'

推荐阅读