2012-08-09 2 views
0

최근 판매 된 5 개의 고유 항목을 자홍색으로 가져오고 싶습니다. 아래 코드가 있습니다. 빈 페이지가 표시되지만 아래 코드에서 ->getSelect()->group('product_id')을 제거하면 작동하지만 항목이 더 이상 고유하지 않습니다.최근 판매 된 5 개의 고유 제품

$itemsCollection= Mage::getResourceModel('sales/order_item_collection') 
     ->join('order', 'order_id=entity_id') 
     ->getSelect()->group('product_id') 
     ->setOrder('order_id', 'desc') 
     ->setPage(1, 5) ; 

이 제품들은 어떻게 그룹화합니까?

+0

당신이 출력의 예를 줄 수 ...

따라서 동작하는 예제가 될 것이다 (또한 가장 최근의 주문 대신 ORDER_ID의 created_at에 의해 주문하는 안전주의) ? – Matt

답변

2

코드가 거의 정확합니다. 문제는 메서드 호출을 연결하는 방법에 달려 있습니다.

체인 메서드 호출시 각 호출은 개체 (일반적으로 같은 클래스에 대한 참조)를 반환합니다. 따라서 귀하의 예에서는 ->getSelect()->group() 호출에서 문제가 발생했습니다. 이는 예상했던대로 Varien_Db_Select 객체가 아닌 Mage_Sales_Model_Resource_Order_Item_Collection 객체에 대한 참조를 반환하기 때문입니다.

// Initialise the collection 
$itemsCollection = Mage::getModel('sales/order_item')->getCollection() 
    ->setOrder('created_at', Varien_Data_Collection::SORT_ORDER_DESC) 
    ->addAttributeToSelect('*') //Change this to only select the fields you require 
    ->setPage(1, 5) 
; 

// Separately add the GROUP BY clause 
$itemsCollection->getSelect()->group('product_id'); 

// Now you can safely iterate over the collection 
foreach($itemsCollection as $item) { 
    echo $item->getData('product_id') . ' - ' . $item->getData('created_at') . '<br/>'; 
} 
관련 문제