by
7,300 18
0
5
446k
70
Top 1% !
Popular
Pearl of Wisdom
Refined
OpenSource
Popularity: 1674th place
No tags for this snippet yet.
Architecturex86_64
Extensionphp
LanguagePHP
LicenseMIT_X11
OSWindows
Windows VersionWindows8

Magento - custom table join product eav table in a grid

Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/347882/Magento---custom-table-join-product-eav-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
protected function _prepareCollection() { //to join normal attributes; but not sku, type_id and others (see catalog_product_entity database table) $productAttributesArray = array('product_name' => 'name', 'product_seller' => 'seller', 'product_supplier' => 'supplier'); $poTempModel = Mage::getModel('mymodule/modelone')->load($this->getRequest()->getParam('id'),'actual_id'); $collection = Mage::getModel('mymodule/modeltwo')->getCollection()->addFieldToFilter('parent_id',$poTempModel->getId()); foreach ($productAttributesArray as $alias => $attributeCode) { $tableAlias = $attributeCode . '_table'; $attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode); //Add eav attribute value $collection->getSelect() ->joinLeft( array($tableAlias => $attribute->getBackendTable()), "main_table.product_id = $tableAlias.entity_id AND $tableAlias.attribute_id={$attribute->getId()}", array($alias => 'value') ); } //to join entity attributes such sku, type_id $collection->join( 'catalog/product', 'product_id = `catalog/product`.entity_id',//note the ` . DO NOT USE ' array('sku' => 'sku') ); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('position', array( 'header'=> 'Position', 'width' => '50px', 'type' => 'number', 'index'=>'position', 'sortable' => false )); $this->addColumn('product_name', array( 'header'=> 'Name', 'type' => 'text', 'index'=>'product_name', 'sortable' => false )); $this->addColumn('sku', array( 'header'=> 'SKU', 'type' => 'text', 'index'=>'sku', 'sortable' => false )); $this->addColumn('poitemtemp_id', array( 'header'=> 'Action', 'align' => 'center', 'type' => 'number', 'index'=>'poitemtemp_id', 'renderer' => 'mymodule/adminhtml_editpo_elements_edit_gridscontainer_currentitemsrender', 'sortable' => false )); return parent::_prepareColumns(); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus