Добавить столбец в сетку клиентов на основе модели модуля


Необходимо добавить пользовательский столбец в сетку Клиентов из статуса в модели модуля.

Я использовал этот код

Config.xml

<adminhtml>
    <rewrite>
<customer_grid>Company_MyModule_Block_Adminhtml_Customer_Grid</customer_grid>
    </rewrite>
</adminhtml>

Company/MyModuleBlock/Adminhtml/Customer/Grid.php

public function setCollection($collection)
{
    //This is where the problem occurs
    $collection->getSelect()->joinLeft(
        array(
            'mycustomtable' => 'my_custom_table'
        ),
    'mycustomtable.customer_id = e.entity_id',
    );
    //This is where the problem occurs
    parent::setCollection($collection);
}
protected function _prepareColumns()
 {
     $this->addColumnAfter('custom_status',
         array(
             'header'=> Mage::helper('catalog')->__('Custom Status'),
             'index' => 'mytable_status',
             'align' => 'center',
             'width' => '20'
         ),
         'website_id'
     );


     return parent::_prepareColumns();
 }

Столбец добавлен, и я могу его просмотреть. я могу заполнить данные из той же таблицы (клиенты), но я не могу заполнить данные из любой другой таблицы..(my_custom_table)

(моя пользовательская таблица)

Спасибо..

Author: Chaitanya, 2017-05-15

2 answers

Просто примечание... пока это не требуется, не используйте перезаписи. Добавление столбцов может быть выполнено с помощью событий/наблюдателей...

<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <your_observer_name>
                    <type>model</type>
                    <class>your_model/observer</class>
                    <method>appendColumnToCustomerGrid</method>
                </your_observer_name>
            </observers>
        </core_block_abstract_prepare_layout_before>
        <eav_collection_abstract_load_before>
            <observers>
                <your_observer_name>
                    <class>your_model/observer</class>
                    <method>beforeCollectionLoad</method>
                </your_observer_name>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>

Company/MyModule/Model/Observer.php

public function appendColumnToCustomerGrid(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();
    if (!isset($block)) {
        return $this;
    }

    if ($block->getType() == 'adminhtml/customer_grid') {
        /* @var $block Mage_Adminhtml_Block_Customer_Grid */
        $block->addColumnAfter('mytable_status', array(
            'header'=> Mage::helper('catalog')->__('Custom Status'),
            'index' => 'mytable_status',
            'align' => 'center',
            'width' => '20'
        ), 'website_id');
    }
}

Здесь использован код от Джеймина Сутарии!

public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if (!isset($collection)) {
        return;
    }

    if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
        /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
        $collection->getSelect()->joinLeft(
            array(
                'mycustomtable' => 'my_custom_table'
            ),
            'mycustomtable.customer_id = e.entity_id',
            array('mycustomtable.mytable_status') // YOUR STATUS COLUMN
        );
    }
}
 1
Author: sv3n, 2017-05-16 00:35:10

Попробуйте обновить свою коллекцию наборов ()

public function setCollection($collection)
{
    //This is where the problem occurs
    $collection->getSelect()->joinLeft(
        array(
            'mycustomtable' => 'my_custom_table'
        ),
    'mycustomtable.customer_id = e.entity_id',
    array('mycustomtable.mytable_status') // YOUR STATUS COLUMN
    );
    //This is where the problem occurs
    parent::setCollection($collection);
}
 1
Author: Jaimin Sutariya, 2017-05-15 06:39:01