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

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();
}

Related

How to get columns in Export CSVs in ss_report Silverstripe?

class Report1 extends SS_Report
{
public function columns()
{
$fields = array(
'Date' => 'Date',
'Method' => 'Method',
'Number' => ' No(NEW)',
);
return $fields;
}
}
I needs to export these columns to csv. And i already define a getExportFields() in particular model. If i going to download csv here output gives defined feilds in getExportFields(). So can anyone help to solve this

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!!

Displaying attributes from another model using CGRIDVIEW

I have 2 tables/models:
Tmp1
Header
QuestionText
Tmp2
Header
Part
OutOf
I'm trying to display the columns: Header, QuestionText, Part, OutOf
in a single CGRIDVIEW.
In Tmp1 model:
public function relations()
{
return array(
'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),
);
}
In Tmp2 Model:
public function relations()
{
return array(
'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),
);
}
Controller:
public function actionReviewAll()
{
$tmp1 = new Tmp1('search');
$tmp1->unsetAttributes();
if(isset($_GET['Tmp1'])){
$model->attributes=$_GET['Tmp1'];
}
$this->render('ReviewAll',array(
'tmp1'=>$tmp1,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
array(
'name' => 'tmp2.OutOf',
'value' => '$data->tmp2[0].OutOf',
'type' => 'raw'
),
),
)); ?>
Error:
Property "Tmp1.tmp2" is not defined.
Any help is greatly appreciated
You have a sintax error:
Tmp1.tmp2 = Tmp1.tmp2s
The relation alias is with 's'. "tmp2s".
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
'tmp2s.Part',
'tmp2s.OutOf',
),
)); ?>
But you are trying to show a 'self::HAS_MANY' relationship, you can't show that on a normal CGridView widget...
Your code probably works (I haven't tried it), but not in cases where your tmp1 model has no tmp2's. You must make sure there is a tmp2 before accessing it:
'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',
You can also pass a function($data,$row) to value to make it look less messy. (Yes, that's important to me.)

How to display one CGridView from two databases

i am trying to display two database tables source in one CGridView..
2 tables were reg.students and login.user..
my students model relation is,
public function relations()
{
Yii::app()->getModule('user');
return array(
'royaltyOutstandings' => array(self::HAS_MANY, 'RoyaltyOutstanding', 'studentID'),
'srkMedicalInfos' => array(self::HAS_MANY, 'SrkMedicalInfo', 'studentID'),
'parents' => array(self::HAS_ONE, 'SrkParents', 'studentID'),
'srkStudentWorksheets' => array(self::HAS_MANY, 'SrkStudentWorksheet', 'studentID'),
'user' => array(self::BELONGS_TO, 'User', 'centre_id'),
);
}
In user module,
public function tableName()
{ return 'login.user'; }
in cgridview columns array,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'students-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'No.',
'value' => '$row+1',
),
array('name' => 'user.centre_id',
'value'=>'$data->user->centre_id',
),
'... // & so on
Controller action is,
public function actionAdmin()
{
$this->layout = 'column3';
$form = new Reports ;
$model=new Students('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Students']))
$model->attributes=$_GET['Students'];
$this->render('admin',array(
'model'=>$model,
'form'=>$form,
));
}
If I understand you correctly, you want to show one or more relations in your gridview.
If this is what you want, have a look at this tutorial: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
This tutorial doesn't only show you how to show a relation in your gridview but it also shows you how to sort and filter the data. good luck!

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

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',
));

Categories