1

I'm trying to extend Products in carts report grid. I needed to separate products for each customer and add a Customer name column. So for example a product with id number equal to 123 may exist in two different carts which means for two individual customers. So I need two rows for product id=123.

But at the moment when this happens I get a familiar message:

Item (Mage_Sales_Model_Quote) with the same id "123" already exist

I tried to rewrite my query starting from sales_flat_quote_item as main table (to have unique entities) but since I'm extending Mage_Reports_Model_Resource_Quote_Collection this fails with this error message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.entity_id' 
in 'field list', query was: SELECT STRAIGHT_JOIN COUNT(DISTINCT main_table.entity_id)
FROM `sales_flat_quote_item` AS `main_table` ...

Currently my prepareForProductsInCarts function starts with:

$collection = Mage::getResourceSingleton('sales/quote_item_collection');

and ends with:

// reset methods may be redundant
$this->getSelect()
    ->useStraightJoin(true)
    ->reset(Zend_Db_Select::COLUMNS)
    ->reset(Zend_Db_Select::DISTINCT)
    ->reset(Zend_Db_Select::GROUP)
    ->reset(Zend_Db_Select::WHERE);
$this->getSelect()->setPart(Zend_Db_Select::COLUMNS, $collection->getSelect()->getPart(Zend_Db_Select::COLUMNS));
$this->getSelect()->setPart(Zend_Db_Select::FROM, $collection->getSelect()->getPart(Zend_Db_Select::FROM));
$this->getSelect()->setPart(Zend_Db_Select::GROUP, $collection->getSelect()->getPart(Zend_Db_Select::GROUP));
$this->getSelect()->setPart(Zend_Db_Select::WHERE, $collection->getSelect()->getPart(Zend_Db_Select::WHERE));

What can I do to allow or work around duplicated entities?

0

I was extending Mage_Reports_Model_Resource_Quote_Collection:

class Name_Sapce_Model_Resource_Reports_Quote_Collection
    extends Mage_Reports_Model_Resource_Quote_Collection {

and after going through base codes I found the only thing I need to change to set my own resource model is invoking _init() from _construct method this way:

protected function _construct()
{
    Mage_Core_Model_Resource_Db_Collection_Abstract::_construct();
    $this->_init('sales/quote_item');
}

An escape to call parent::_construct(); may be necessary and that's the reason why I used parent's parent class directly.

Now main_table correctly is an alias for sales_flat_quote_item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.