1

カスタム モジュールを使用してクラスを拡張し、Mage_Adminhtml_Block_Sales_Order_Gridいくつかの顧客属性 (Magento EE 1.10) をグリッドに追加しました。

次のような 3 つの結合を使用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_code更新: 注文を表示するとすべて正常に表示されますが、テキスト結合フィールド (または)で注文を検索/フィルターしようとするとうまくいきませんdid_number。結果は SQL エラーです: " SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous."

この問題は、ステートメントの 1 つを除くすべてを削除した場合にも存在するleftJoin()ため、テーブルとの結合の両方 (いずれか) で問題が発生していcustomer_entity_varcharます。

4

4 に答える 4

2

store_id という名前の列が 2 つあるためfilter_index、列をグリッドに追加するタイミングを指定する必要があります。

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

));

フィルタリング中に参照しているものを認識できるようにします。

それが役立つことを願っています!

于 2014-12-08T13:25:47.717 に答える
1

おそらくそれは、あなたがcustomer_entity_varchar_table2 回参加しているからです。

$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

問題は 2 つの異なる場所に存在するようです。1 つのケースは、1 つのストアを持つユーザーとしてログインした場合であり、もう 1 つのケースは、さまざまなストアをフィルター処理できるユーザーとしてログインした場合です。

単一店舗ユーザー

addAttributeToFilter私が行った解決策は、コレクション クラスのメソッドをオーバーライドすることでした。メソッドの変更が他の動作にどのような影響を与えるかを正確に把握してEnterprise_AdminGws_Model_Collections::addStoreAttributeToFilterいないため、それを避けたかったのですが、Mage_Adminhtml_Block_Sales_Order_GridJavierが提案したようにフィルター インデックスを追加しても機能しないことがわかりました。

代わりに、次のメソッドを に追加しました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 に答える