Nếu bạn muốn một truy vấn thực tế, có lẽ bạn có thể làm điều gì đó đơn giản như (bổ sung thêm tham gia để có được thông tin khách hàng từ EAV):
SELECT DISTINCT o.customer_id FROM sales_flat_order_item i
INNER JOIN sales_flat_order o ON o.entity_id = i.order_id
WHERE o.customer_id IS NOT NULL
AND i.sku = 'some-product-sku'
Sử dụng mô hình Magento, điều này sẽ làm việc cho bạn:
<?php
require_once 'app/Mage.php';
/*
* Initialize Magento. Older versions may require Mage::app() instead.
*/
Mage::init();
/**
* Get all unique order IDs for items with a particular SKU.
*/
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addFieldToFilter('sku', 'some-product-sku')
->toArray(array('order_id'));
$orderIds = array_unique(array_map(
function($orderItem) {
return $orderItem['order_id'];
},
$orderItems['items']
));
/**
* Now get all unique customers from the orders of these items.
*/
$orderCollection = Mage::getResourceModel('sales/order_collection')
->addFieldToFilter('entity_id', array('in' => $orderIds))
->addFieldToFilter('customer_id', array('neq' => 'NULL'));
$orderCollection->getSelect()->group('customer_id');
/**
* Now get a customer collection for those customers.
*/
$customerCollection = Mage::getModel('customer/customer')->getCollection()
->addFieldToFilter('entity_id', array('in' => $orderCollection->getColumnValues('customer_id')));
/**
* Traverse the customers like any other collection.
*/
foreach ($customerCollection as $customer) {
var_dump($customer->getData());
}
Mặc dù khá xấu xí (instantiates nhiều mô hình, thực hiện một loạt các truy vấn dưới bìa), bạn có thể viết mô hình của riêng bạn để làm cho điều này trở nên đẹp hơn.
Nguồn
2012-03-01 15:13:55
Cảm ơn rất nhiều, tôi sẽ làm điều đó nhưng dường như tất cả hoàn hảo để làm việc. Và có trang web nào mà tôi có thể tìm thấy tất cả các bảng hay không. Ý tôi là vai trò của mỗi bảng là gì. – RIK