使用多个_prepareCollection()

时间:2013-04-08 11:01:44

标签: magento collections

获得此代码(在:app / code / local / Mage / Adminhtml / Block / Sales / Order / Grid.php中):

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR "<br>")')))->group('sales_flat_order_item.entity_id');
        $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'))->group('sales_flat_order_payment.parent_id');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

当我评论COL 1或COL 2下面的行时,这可以正常工作,但我希望它们能够两者,这是怎么做到的? (如果我在同时使用它们时收到错误:Integrity constraint violation: 1052 Column 'entity_id' in group statement is ambiguous

更新答案:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR "<br>")')));
    $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'))->group('main_table.entity_id');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

2 个答案:

答案 0 :(得分:2)

答案是在错误中,应用组的表无法确定。您需要明确定义表:

...->group('sales_flat_order_item.entity_id');

答案 1 :(得分:0)

使用echo(string)$ collection-&gt; getSelect();回显您的查询您将在此处获得一个简单的SQL查询。 在数据库中查询并检查,您是否以您想要的方式获得结果。 最后但并非最不重要的组也会产生问题。 看看下面的链接 using group() breaks getSelectCountSql in magento