How To Call Another Model's Function By Passing Params In A Cgridview

Hi, i am newbii to YII. i am facing a problem in passing params to a function. What i am doing is.

I have a model "IMEI" for which i am showing its data in a CGridview.

Now i am adding a new column "Product Name" in it. The "product name " will be fetched from another Model "Inventory" by "getProductName" function. i have to pass "product_id" to this function but unable to understand that how i can pass "product_id" to this function. please guide me. The code is given below.

Model: IMEI





* @property string $id

 * @property integer $po_item_id

 * @property integer $sale_item_id

 * @property integer $product_id

 * @property string $imei

 * @property string $serial

 * @property string $sim_card

 * @property string $status

 * @quantity string $quantity

 */

class Imei extends CActiveRecord

{

	public $quantity;


}



Model: inventory





/**

 * The followings are the available columns in table 'inventory':

 * @property integer $id

 * @property integer $product_id

 * @property integer $quantity

 * @property integer $threshold

 */

class Inventory extends CActiveRecord

{

        public function getProductName($product_id=null)

	{

		$sql = "select description as product_name from purchase_product where product_id=".$this->product_id;		

		$command = yii::app()->db->createCommand($sql);

		$product_data = $command->queryRow();

		return $product_data['product_name'];

	}

}




view.php





$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'inventory-ebay-grid',

	'dataProvider'=>$imei_model->searchImei(),

	'filter'=>$imei_model,

	'columns'=>array(

		array(

			'name' => 'product_id',

			'header' => 'Product ID'

		),

		array(

			'name' => 'product_id',

			'header'	=>	'Product Name',

			'value'	=>	'Inventory::model()->getProductName($data->id)'

		),

			

		'quantity',

		'status',

	),

));




You should be using product_id:




'value'=>'Inventory::model()->getProductName($data->product_id)'



Hi,

Add type as raw in your view.php file as follows





$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'inventory-ebay-grid',

        'dataProvider'=>$imei_model->searchImei(),

        'filter'=>$imei_model,

        'columns'=>array(

                array(

                        'name' => 'product_id',

                        'header' => 'Product ID'

                ),

                array(

                        'name' => 'product_id',

			'type' => 'raw',

                        'header'        =>      'Product Name',

                        'value' =>      'Inventory::model()->getProductName($data->product_id)'

                ),

                        

                'quantity',

                'status',

        ),

));




Don’t add ‘type’=>‘raw’. That will prevent your product name from being HTML encoded, opening a security hole.

Oh yes… Thanks keith. Sorry ignore it…