Неустранимая ошибка в методе "Preparecolumns"
Получение фатальной ошибки при Item (Mage_Catalog_Model_Product) with the same id "223" already exist
Здесь 223
мой идентификатор продукта
Мой _prepareCollection()
Метод
protected function _prepareCollection()
{
$coll = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('name',array('neq'=>''))
->addAttributeToFilter('attribute_code',$this->getRequest()->getParam('my_value'));
$coll->getSelect()->join( array('order_item'=> 'mgnt_sales_flat_order_item'), 'order_item.product_id = e.entity_id', array('order_item.created_at as ocreated','order_item.qty_ordered as sales_units', 'order_item.row_total as sales_total' , 'order_item.discount_amount as discount', 'order_item.item_id as item_id' ) );
$coll->getSelect()->join( array('order'=> 'mgnt_sales_flat_order'), 'order.entity_id = order_item.order_id', array('customer_name'=>'order.customer_firstname'));
$this->setCollection($coll);
// echo $coll->getSelect();
return parent::_prepareCollection();
}
Мой _prepareColumns()
метод
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => $this->_getHelper()->__('entity_id'),
'type' => 'text',
'index' => 'entity_id',
'filter' => false,
'sortable' => false
));
$this->addColumn('name', array(
'header' => $this->_getHelper()->__('Product Name'),
'type' => 'text',
'index' => 'name',
'filter' => false,
'sortable' => false
));
$this->addColumn('customer_name', array(
'header' => $this->_getHelper()->__('customer_name'),
'type' => 'number',
'index' => 'customer_name',
'filter' => false,
'sortable' => false
));
$this->addColumn('sales_total', array(
'header' => $this->_getHelper()->__('Sales Total'),
'type' => 'number',
'index' => 'sales_total',
'filter' => false,
'sortable' => false
));
$this->addColumn('discount', array(
'header' => $this->_getHelper()->__('Discount'),
'type' => 'number',
'index' => 'discount',
'filter' => false,
'sortable' => false
));
return parent::_prepareColumns();
}
1
Author: Murtuza Zabuawala, 2017-03-06
1 answers
Согласно предложенной ссылке гопала "Вы добавили что-то в коллекцию, что добавляет дубликаты в результат запроса. Что magento делает с результатом запроса, так это создает объект из каждой строки, а затем добавляет эти элементы в коллекцию. Если элемент уже существует, возникает эта ошибка"
.В качестве взлома вы можете попробовать это для решения в своем методе _prepareCollection()
:
$resource = Mage::getModel('core/resource');
$connection = $resource->getConnection('core_read');
$sql = $coll->getSelect();
$rows = $connection->fetchAll($sql);//this row will return an array
$collection = new Varien_Data_Collection();
foreach($rows as $row){
$rowObj = new Varien_Object();
$rowObj->setData($row);
$collection->addItem($rowObj);
}
$this->setCollection($collection);
return parent::_prepareCollection();
4
Author: Ashish Jagnani, 2017-03-06 12:08:02