Aggiungi colonna a una griglia (osservatore) - Colonna ‘STORE_ID’ in cui la clausola è questione ambigua
-
16-10-2019 - |
Domanda
Sto aggiungendo una colonna alla griglia di ordine utilizzando approccio osservatore:
- Nella manifestazione ->
sales_order_grid_collection_load_before
Sto aggiungendo un join alla collezione - Nella manifestazione ->
core_block_abstract_prepare_layout_before
Sto aggiungendo una colonna alla griglia
Modifica Ulteriori informazioni:
On Event (1):
public function salesOrderGridCollectionLoadBefore($observer)
{
$collection = $observer->getOrderGridCollection();
$collection->addFilterToMap('store_id', 'main_table.store_id');
$select = $collection->getSelect();
$select->joinLeft(array('oe' => $collection->getTable('sales/order')), 'oe.entity_id=main_table.entity_id', array('oe.customer_group_id'));
}
On Event (2):
public function appendCustomColumn(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/sales_order_grid') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$this->_addColumnToGrid($block);
}
}
protected function _addColumnToGrid($grid)
{
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionHash();
$groups[0] = 'Guest';
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$grid->addColumnAfter('customer_group_id', array(
'header' => Mage::helper('customer')->__('Customer Group'),
'index' => 'customer_group_id',
'filter_index' => 'oe.customer_group_id',
'type' => 'options',
'options' => $groups,
), 'shipping_name');
}
Tutto bene fino a quando il lavoro a filtrare la griglia con filtro di visualizzazione negozio: colonna ‘STORE_ID’ in cui la clausola è ambigua problema
Ho stampato la domanda:
SELECT `main_table`.*, `oe`.`customer_group_id`
FROM `sales_flat_order_grid` AS `main_table`
LEFT JOIN `sales_flat_order` AS `oe` ON oe.entity_id=main_table.entity_id
WHERE (store_id = '5') AND (oe.customer_group_id = '6')
Come si CASE Vedi store_id
perdere main_table
alias.
Per fare questo ho solo bisogno di impostare la filter_index
per negozio colonna ID, ma per l'osservatore
Quindi la domanda è: come posso farlo al volo ?
senza sostituire il blocco di classe? (Altrimenti l'approccio osservatore è inutile)
Soluzione
Proviamo di nuovo con il altra soluzione ho detto prima a voi :-), ho costruire l'estensione completa per mostrare come aggiungere il campo alla tavola griglia. Dopo di che è necessario solo un file di aggiornamento di layout per aggiungere la colonna per ordinare la pagina della griglia.
ho chiamato l'estensione Example_SalesGrid, ma è possibile modificarlo per le proprie esigenze.
La partenza di Let creando il modulo init XML in /app/etc/modules/Example_SalesGrid.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!--
Module bootstrap file
-->
<config>
<modules>
<Example_SalesGrid>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Sales />
</depends>
</Example_SalesGrid>
</modules>
</config>
Avanti creiamo il nostro modulo di configurazione XML in /app/code/community/Example/SalesGrid/etc/config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Example_SalesGrid>
<version>0.1.0</version> <!-- define version for sql upgrade -->
</Example_SalesGrid>
</modules>
<global>
<models>
<example_salesgrid>
<class>Example_SalesGrid_Model</class>
</example_salesgrid>
</models>
<blocks>
<example_salesgrid>
<class>Example_SalesGrid_Block</class>
</example_salesgrid>
</blocks>
<events>
<!-- Add observer configuration -->
<sales_order_resource_init_virtual_grid_columns>
<observers>
<example_salesgrid>
<model>example_salesgrid/observer</model>
<method>addColumnToResource</method>
</example_salesgrid>
</observers>
</sales_order_resource_init_virtual_grid_columns>
</events>
<resources>
<!-- initialize sql upgrade setup -->
<example_salesgrid_setup>
<setup>
<module>Example_SalesGrid</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
</example_salesgrid_setup>
</resources>
</global>
<adminhtml>
<layout>
<!-- layout upgrade configuration -->
<updates>
<example_salesgrid>
<file>example/salesgrid.xml</file>
</example_salesgrid>
</updates>
</layout>
</adminhtml>
</config>
Ora creiamo lo script di aggiornamento di SQL in /app/code/community/Example/SalesGrid/sql/example_salesgrid_setup/install-0.1.0.php :
<?php
/**
* Setup scripts, add new column and fulfills
* its values to existing rows
*
*/
$this->startSetup();
// Add column to grid table
$this->getConnection()->addColumn(
$this->getTable('sales/order_grid'),
'customer_group_id',
'smallint(6) DEFAULT NULL'
);
// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
$this->getTable('sales/order_grid'),
'customer_group_id',
'customer_group_id'
);
// Now you need to fullfill existing rows with data from address table
$select = $this->getConnection()->select();
$select->join(
array('order'=>$this->getTable('sales/order')),
$this->getConnection()->quoteInto(
'order.entity_id = order_grid.entity_id'
),
array('customer_group_id' => 'customer_group_id')
);
$this->getConnection()->query(
$select->crossUpdateFromSelect(
array('order_grid' => $this->getTable('sales/order_grid'))
)
);
$this->endSetup();
Poi creiamo il file di aggiornamento layout in /app/design/adminhtml/default/default/layout/example/salesgrid.xml:
<?xml version="1.0"?>
<layout>
<!-- main layout definition that adds the column -->
<add_order_grid_column_handle>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>customer_group_id</columnId>
<arguments module="sales" translate="header">
<header>Customer Group</header>
<index>customer_group_id</index>
<type>options</type>
<filter>Example_SalesGrid_Block_Widget_Grid_Column_Customer_Group</filter>
<renderer>Example_SalesGrid_Block_Widget_Grid_Column_Renderer_Customer_Group</renderer>
<width>200</width>
</arguments>
<after>grand_total</after>
</action>
</reference>
</add_order_grid_column_handle>
<!-- order grid action -->
<adminhtml_sales_order_grid>
<!-- apply the layout handle defined above -->
<update handle="add_order_grid_column_handle" />
</adminhtml_sales_order_grid>
<!-- order grid view action -->
<adminhtml_sales_order_index>
<!-- apply the layout handle defined above -->
<update handle="add_order_grid_column_handle" />
</adminhtml_sales_order_index>
</layout>
Ora abbiamo bisogno di due file di blocchi, uno per creare le opzioni del filtro, /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Customer/Group.php:
<?php
class Example_SalesGrid_Block_Widget_Grid_Column_Customer_Group extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select {
protected $_options = false;
protected function _getOptions(){
if(!$this->_options) {
$methods = array();
$methods[] = array(
'value' => '',
'label' => ''
);
$methods[] = array(
'value' => '0',
'label' => 'Guest'
);
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionArray();
$this->_options = array_merge($methods,$groups);
}
return $this->_options;
}
}
E la seconda di tradurre i valori di riga al testo corretto che verrà visualizzato, /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Renderer/Customer/Group.php :
<?php
class Example_SalesGrid_Block_Widget_Grid_Column_Renderer_Customer_Group extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
protected $_options = false;
protected function _getOptions(){
if(!$this->_options) {
$methods = array();
$methods[0] = 'Guest';
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionHash();
$this->_options = array_merge($methods,$groups);
}
return $this->_options;
}
public function render(Varien_Object $row){
$value = $this->_getValue($row);
$options = $this->_getOptions();
return isset($options[$value]) ? $options[$value] : $value;
}
}
L'ultimo file necessario è necessaria solo se si crea una colonna in più da una tabella diversa vendite / ordine (sales_flat_order). Tutti i campi in vendita / order_grid corrispondente al nome della colonna da vendite / ordine viene aggiornato automaticamente nella vendita / order_grid tavolo. Se è necessario aggiungere l'opzione di pagamento, ad esempio avrete bisogno di questo osservatore per aggiungere il campo alla query in modo che i dati possono essere copiati nella tabella corretta. L'osservatore utilizzato per questo è in /app/code/community/Example/SalesGrid/Model/Observer.php :
<?php
/**
* Event observer model
*
*
*/
class Example_SalesGrid_Model_Observer {
public function addColumnToResource(Varien_Event_Observer $observer) {
// Only needed if you use a table other than sales/order (sales_flat_order)
//$resource = $observer->getEvent()->getResource();
//$resource->addVirtualGridColumn(
// 'payment_method',
// 'sales/order_payment',
// array('entity_id' => 'parent_id'),
// 'method'
//);
}
}
Questo codice è basato sull'esempio da http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4 -1.html
La speranza l'esempio precedente risolve il problema.
Altri suggerimenti
Prova ad utilizzare questi:
public function salesOrderGridCollectionLoadBefore($observer)
{
/**
* @var $select Varien_DB_Select
*/
$collection = $observer->getOrderGridCollection();
$collection->addFilterToMap('store_id', 'main_table.store_id');
$select = $collection->getSelect();
$select->joinLeft(array('oe' => $collection->getTable('sales/order')), 'oe.entity_id=main_table.entity_id', array('oe.customer_group_id'));
if ($where = $select->getPart('where')) {
foreach ($where as $key=> $condition) {
if (strpos($condition, 'store_id')) {
$value = explode('=', trim($condition, ')'));
$value = trim($value[1], "' ");
$where[$key] = "(main_table.store_id = '$value')";
}
}
$select->setPart('where', $where);
}
}
Avete veramente bisogno nel metodo salesOrderGridCollectionLoadBefore
il seguente codice $collection->addFilterToMap('store_id', 'main_table.store_id');
? Se non rimuoverlo e provare quanto segue:
protected function _addColumnToGrid($grid)
{
....... // here you code from your post above
$storeIdColumn = $grid->getColumn('store_id');
if($storeIdColumn) {
$storeIdColumn->addData(array('filter_index' => 'main_table.store_id'));
}
}
Invece di usare il nome della colonna statica è possibile utilizzare il metodo seguito per tutti colonna. Posso capire se la risposta l'utilizzo mageUz che lavorerà per una colonna e se si va per qualche altra colonna allora si potrebbe ottenere lo stesso errore. Così in basso codice che dà soluzione per tutte le colonne contemporaneamente.
public function salesOrderGridCollectionLoadBefore(Varien_Event_Observer $observer)
{
$collection = $observer->getOrderGridCollection();
$select = $collection->getSelect();
$select->joinLeft(array('order' => $collection->getTable('sales/order')), 'order.entity_id=main_table.entity_id',array('shipping_arrival_date' => 'shipping_arrival_date'));
if ($where = $select->getPart('where')) {
foreach ($where as $key=> $condition) {
$parsedString = $this->get_string_between($condition, '`', '`');
$yes = $this->checkFiledExistInTable('order_grid',$parsedString);
if($yes){
$condition = str_replace('`','',$condition);
$where[$key] = str_replace($parsedString,"main_table.".$parsedString,$condition);
}
}
$select->setPart('where', $where);
}
}
public function checkFiledExistInTable($entity=null,$parsedString=null){
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
if($entity == 'order'){
$table = 'sales/order';
}elseif($entity == 'order_grid'){
$table = 'sales/order_grid';
}else{
return false;
}
$tableName = $resource->getTableName($table);
$saleField = $readConnection->describeTable($tableName);
if (array_key_exists($parsedString,$saleField)){
return true;
}else{
return false;
}
}
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}