Add custom column in Magento Grid which field taken from xref table - php

In Magento Grid, I need to add a custom column (section_name) which field is taken from another database table through intermediary xref table. In Grid.php I tried with these codes:
protected function _prepareCollection() {
$collection = Mage::getModel('module/items')->getCollection()
->getSelect()->join('section_name',
'module/sections',
'section_name',
'table_sections.section_id=table_sections_items_xref.section_id',
'{{table}}.item_id=table_sections_items_xref.item_id',
'left'
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
$this->addColumn('section_name', array (
'header' => Mage::helper('module')->__('Section Name'),
'type' => 'text',
'index' => 'section_name',
));

Related

how to specify table name in addColumn() function in magento

I am trying to get values from a table but don't know where to specify table name in addColumn() function in magento. Kindly help.
Following is the code:
protected function _prepareColumns()
{
$this->addColumn('orders_count', array(
'header' => Mage::helper('sales')->__('Orders'),
'index' => 'orders_count',
'type' => 'number',
'total' => 'sum',
'sortable' => false
));
}
It looks like your Grid code
Whenever Grid is loading firstly it will load a function "_prepareCollection()" where we collect collection data to show in grid. There we define our resource model(or table).
Example:
protected function _prepareCollection() {
$collection = Mage::getResourceModel('module/table_collection');
$this->setCollection($collection);
return parent::_prepareCollection();
}

Add product name with attribute color, size in Order Grid Magento

I want to add Product name with attributes (color,size). But after using below code total record show only 2 records. Here is my code:
file name is : app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft('sales_flat_order', 'main_table.entity_id = sales_flat_order.entity_id',array('customer_email'=>'customer_email'));
$collection->getSelect()->joinLeft('sales_order_status', 'sales_flat_order.status=sales_order_status.status',array('label'=>'label'));
$collection->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'product_name' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
);
$collection->getSelect()->group('entity_id');
//$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id AND sales_flat_order_address.address_type = "shipping" ',array('telephone', 'email'));
//var_dump((string)$collection->getSelect());
$this->setCollection($collection);
return parent::_prepareCollection();
}
and in function _prepareColumns()
$this->addColumn('product_name', array(
'header' => Mage::helper('Sales')->__('Product Name'),
'width' => '100px',
'index' => 'product_name',
'type' => 'text',
));
Kindly help me how to add attributes with product name?

Yii CGridView, I can't display fields form other model using HAS_MANY relationship

I have a HAS_MANY relation between 2 models, i use bookID as foreign key
model 1 - Importedbooks, Importedbooks can have many CountryOfImport
public function relations()
{
return array(
'CountryOfImportObj'=>array(self::HAS_MANY, 'CountryOfImport', 'bookID')
);
}
model 2 CountryOfImport, CountryOfImport belongs to Importedbooks
public function relations()
{
return array(
'ImportBooksObj'=>array(self::BELONGS_TO, 'Importedbooks', 'bookID')
);
}
Now, For my CGridView i am using a model->search()of the Importedbooks model as my dataProvider, From here is where i get stuck.
CGridView
$data = $model->search();
$data->setPagination(array('pageSize'=>'5'));
$data->criteria->addCondition('active = "yes"');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$data
,'filter'=>$model
,'pager'=>array('header'=>'')
,'columns'=>array(
'id',
'bookYear',
'bookTitle',
'DISPLAY VALUE FROM OTHER model HERE'
)
)
);
DataProvider of the Imported books model, i'm using this as my data provider for the grid
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('bookID',$this->bookID);
$criteria->compare('countryName',$this->countryName,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The relation works because i can use below code in my controller to get a field(countryName) from the other model
$model = Importedbooks::model()->with('CountryOfImportObj')->findbyPK(3);
print_r($model->CountryOfImportObj[0]->countryName);
Such as Importedbooks have many CountryOfImport, in one row you must show or all country names or concreet names depending on ID or countryName or some other attribute.
You can pass anonymous function as value:
'columns' => array(
'id',
'bookYear',
'bookTitle',
array(
'header' => 'Countries',
'type' => 'raw',
// if you have defined counrtyName attribute in 'Importedbooks' model, if not you can remove this row
// also CGridView 'uses' attribute 'name' for filtering or sorting
'name' => 'countryName',
'value' => function($data, $row) { //$data is item of DataProvider, $row is number of row (starts from 0)
$countries = CHtml::listData((array)$data->CountryOfImportObj, 'id', 'countryName');
return implode(', ', $countries);
}
)
)
If you want to filter or sort by external attributes too, you must "declare them"
Thanks!

How to get Product name in custom module in magento grid view?

I have product id. database field name productid in custom module
I want to show product name on custom module list view.
Here is my code:
protected function _prepareColumns() {
$this->addColumn('productid', array(
'header' => Mage::helper('productpdf')->__('productid'),
'align' => 'center',
'index' => 'productid',
));
}
For this You have to separately create the collection(or in your existing collection include the below query) for get product name from product id..
You can do like this
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name');
$collection->addAttributeToFilter('productid');
}
protected function _prepareColumns()
{
$this->addColumn('productid', array(
'header' => Mage::helper('productpdf')->__('productid'),
'align' =>'center',
'index' =>'productid',
));
$this->addColumn('name', array(
'header' => Mage::helper('productpdf')->__('Name'),
'width' => '150',
'index' => 'name'
));
}
In addAttributeToFilter('productid') you have to pass your product id.
Hope this work:-)
Instead of using too much bunch of code, add below code in your model/productpdf.php file.
/**
* #param int $productId.
* #return product name.
*/
public function getProductNameById($productId)
{
$query_select = "SELECT value FROM `".Mage::getConfig()->getTablePrefix()."catalog_product_entity_varchar` WHERE
entity_id = '".$productId."' AND `attribute_id` = 71";
return $data = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchAll($query_select);
}
Now just call this function in your file.
$productNameArray = Mage::getModel('productpdf/productpdf')->getProductNameById($productId);
Add new column for display product name
$this->addColumn('name', array(
'header' => Mage::helper('productpdf')->__('Name'),
'width' => '150',
'index' => $productNameArray[0]['value']
));
Cheers!!

CakePHP -> findall without has_many

Model box
public $hasMany = array('Ticket' => array(
'className' => 'Ticket',
'order' => 'Ticket.created DESC',
'foreign_key' => 'box_id'
));
Model Ticket
public $belongsTo = 'Box';
In BoxesController I get data from box table
$this->set('boxes', $this->Box->find('all'));
This function get all data from table Box and from table Ticket.
How can I get data only from one table, without join other tables?
$this->Box->recursive = -1;
$this->set('boxes', $this->Box->find('all'));
or
$this->Box->unbindModel(
array('hasMany' => array('Ticket'))
);
$this->set('boxes', $this->Box->find('all'));
More on recursive

Categories