Yii Framework Forum: How To Call Another Model's Function By Passing Params In A Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Call Another Model's Function By Passing Params In A Cgridview Rate Topic: -----

#1 User is offline   sheraz.s 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 11-June 13

Posted 11 December 2013 - 06:06 AM

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


0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,313
  • Joined: 04-March 10
  • Location:UK

Posted 11 December 2013 - 06:15 AM

You should be using product_id:

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

0

#3 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 25-June 13

Posted 11 December 2013 - 06:17 AM

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


View Postsheraz.s, on 11 December 2013 - 06:06 AM, said:

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



Thanks & Regards,
S.Arockia Johnson
www.arojohnson.tk
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,313
  • Joined: 04-March 10
  • Location:UK

Posted 11 December 2013 - 06:27 AM

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

#5 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 25-June 13

Posted 11 December 2013 - 06:31 AM

Oh yes.. Thanks keith. Sorry ignore it..

View PostKeith, on 11 December 2013 - 06:27 AM, said:

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

Thanks & Regards,
S.Arockia Johnson
www.arojohnson.tk
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users