1

Mage_Adminhtml_Block_Sales_Order_Grid使用自定义模块扩展了该类,以将多个客户属性(Magento EE 1.10)添加到网格中。

我在方法中使用三个连接将自定义属性添加到我的MyCompany_MyModule_Block_Adminhtml_Order_Grid类中的集合中,如下所示:_prepareCollection()

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());

    //get the table names for the customer attributes we'll need
    $customerEntityVarchar = Mage::getSingleton('core/resource')
        ->getTableName('customer_entity_varchar');
    $customerEntityInt = Mage::getSingleton('core/resource')
        ->getTableName('customer_entity_int');
    // add left joins to display the necessary customer attribute values
    $collection->getSelect()->joinLeft(array(
        'customer_entity_int_table'=>$customerEntityInt), 
        '`main_table`.`customer_id`=`customer_entity_int_table`.`entity_id`
            AND `customer_entity_int_table`.`attribute_id`=148', 
        array('bureau'=>'value'));
    $collection->getSelect()->joinLeft(array(
        'customer_entity_varchar_table'=>$customerEntityVarchar), 
        '`main_table`.`customer_id`=`customer_entity_varchar_table`.`entity_id`
            AND `customer_entity_varchar_table`.`attribute_id`=149', 
        array('index_code'=>'value'));
    $collection->getSelect()->joinLeft(array(
        'customer_entity_varchar_2_table'=>$customerEntityVarchar), 
        '`main_table`.`customer_id`=`customer_entity_varchar_2_table`.`entity_id` 
            AND `customer_entity_varchar_2_table`.`attribute_id`=150', 
        array('did_number'=>'value'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

更新:虽然查看订单时一切正常,但当我尝试通过任何文本连接字段(index_codedid_number)搜索/过滤订单时,情况并不好。结果是 SQL 错误:“ SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous.”

如果我删除除一个语句之外的所有leftJoin()语句,也会存在此问题,因此表的两个(任何一个)连接都出现问题customer_entity_varchar

4

4 回答 4

2

由于现在有两个名为 store_id 的列,因此您必须filter_index在将列添加到网格时指定:

$this->addColumn('store_id', array(
  ...
  'filter_index'=>'main_table.store_id',

));

这样它就知道您在过滤时指的是哪一个。

我希望它有帮助!

于 2014-12-08T13:25:47.717 回答
1

很可能是因为您要加入customer_entity_varchar_table两次。

$collection->getSelect()->joinLeft(array(
    'customer_entity_varchar_table'=>$customerEntityVarchar), 
    '`main_table`.`customer_id`=`customer_entity_varchar_table`.`entity_id`
        AND `customer_entity_varchar_table`.`attribute_id`=149', 
    array('index_code'=>'value'));
$collection->getSelect()->joinLeft(array(
    'customer_entity_varchar_2_table'=>$customerEntityVarchar), 
    '`main_table`.`customer_id`=`customer_entity_varchar_2_table`.`entity_id` 
        AND `customer_entity_varchar_2_table`.`attribute_id`=150', 
    array('did_number'=>'value'));

您可能想要结合这些,您也可以尝试打印 SQL 以查看查询的样子:

$collection->getSelect()->getSelectSql();

有关收藏的更多信息:http: //blog.chapagain.com.np/magento-collection-functions/

于 2011-07-06T16:25:30.963 回答
0

问题似乎存在于两个不同的地方。一种情况是,如果以具有单个商店的用户身份登录,另一种情况是作为可以过滤各种商店的用户登录。

单店用户

我采用的解决方案是覆盖addAttributeToFilter集合类上的方法。不确切知道更改Enterprise_AdminGws_Model_Collections::addStoreAttributeToFilter方法会影响我想避免的其他行为,我发现Mage_Adminhtml_Block_Sales_Order_Grid按照 Javier的建议添加过滤器索引不起作用。

相反,我将以下方法添加到Mage_Sales_Model_Resource_Order_Grid_Collection

/**
 * {@inheritdoc}
 */
public function addAttributeToFilter($attribute, $condition = null)
{
    if (is_string($attribute) && 'store_id' == $attribute) {
        $attribute = 'main_table.' . $attribute;
    }
    return parent::addFieldToFilter($attribute, $condition);
}

可以在这里找到补丁:https ://gist.github.com/josephdpurcell/baf93992ff2d941d02c946aeccd48853

多店用户

如果用户可以在 admin/sales_order 按商店过滤订单,则还需要在第 75 行附近对 Mage_Adminhtml_Block_Sales_Order_Grid 进行以下更改:

    if (!Mage::app()->isSingleStoreMode()) {
        $this->addColumn('store_id', array(
            'header'    => Mage::helper('sales')->__('Purchased From (Store)'),
            'index'     => 'store_id',
            'type'      => 'store',
            'store_view'=> true,
            'display_deleted' => true,
            'filter_index' => 'main_table.store_id',
        ));
    } 

可以在此处找到补丁:https ://gist.github.com/josephdpurcell/c96286a7c4d2f5d1fe92fb36ee5d0d5a

于 2016-09-12T22:02:30.353 回答
-1

我有同样的错误,在对代码进行 grep 之后,我终于找到了Enterprise_AdminGws_Model_Collections第 ~235 行的类中的麻烦制造者:

/**
 * Add store_id attribute to filter of EAV-collection
 *
 * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
 */
public function addStoreAttributeToFilter($collection)
{
    $collection->addAttributeToFilter('store_id', array('in' => $this->_role->getStoreIds()));
}

您必须替换'store_id''main_table.store_id',当然您必须在自己的重写中扩展该特定方法以遵守 Magento 指南:p

希望能帮助到你!

于 2013-09-27T07:37:04.460 回答