CGridView CButtonColumn - Chanding the URL Parameter

Hi,

The default Admin View shows a data grid, and each row has a VIEW / UPDATE / DELETE button.

I’ve recently changed my Primary Key, so for the Table concerned, its no longer ‘ID’ its now ‘ID_LEAD’…

So if you click UPDATE, it tries to load

/index.php?r=leads/update&id=7840

But i need it to load

/index.php?r=leads/update&id_lead=7840

I’ve experimented with changing the CButtonColumn in the Admin View:


array

		(

			'class'=>'CButtonColumn',

			'template'=>'{view}{update}{delete}',

			'buttons'=>array

			(

				'update' => array

				(

					'url'=>'"index.php?r=leads/update&id_lead="',

				),

			),

		),

It shows the URL:

/index.php?r=leads/update&id_lead=

but I dont know how to get the value of ‘id_lead’ in there… also it feels abit too clunky to do this…

Can this be set sitewide - im guessing, its built into the Framework, and i obviously dont want to hack that…

:-X

ok, i added this, and the URL works now, but there must be a better way…




array

  (

    'class'=>'CButtonColumn',

    'template'=>'{view}{update}{delete}',

    'buttons'=>array

    (

      'update' => array

        (

        'url'=>'$this->grid->controller->createUrl("/leads/update", array("id_lead"=>$data->primaryKey))',

	//'url'=>'"index.php?r=leads/update&id_lead="',

        ),

     ),

  ),



You can use


'url'=>'CController::createUrl("/leads/update", array("id_lead"=>$data->primaryKey))'

EDIT: As the button column evals the code you really have to give it a string. So use CController to create the url. That should work

Hi ThePaulius

If it can help you the default url are set in this line:

http://code.google.com/p/yii/source/browse/tags/1.1.10/framework/zii/widgets/grid/CButtonColumn.php#189




         * @var string a PHP expression that is evaluated for every update button and whose result is used

         * as the URL for the update button. In this expression, the variable

         * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;

         * and <code>$this</code> the column object.

         */

        public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';




So, you need to configure it manually.

There is however another solution!:)

I was wondering why I never have met this problem. The reason is that I use User-friendly URLs . So that is a way to make the buttons create a valid url even if you changed the name of primaryKey. But I think the User Friendly Urls are good to use for other reasons as well, and quite easy to implement. See the link.

Good Luck

Nice thank you, that worked, shorter too.

Great thank you for finding were id= is created, i am very tempted to change it, but i wont because i know when i update the framework, i will forget that i changed that! lol

Also, thanks for the tip on friendly URLS, I guess that will fix the problem for sure. I do prefer friendly URLS (Hate ?id=xyz …) I tried creating them early in the project, but reverted back becasue all urls still had index.php? which is really weird. I think I tried out something with htaccess rewrites, but gave up.

Cheers

p

Hi i m trying to do the same but a got the error notice "Trying to get property of non-object" even when i m takin off all the attributes of CButtonColumn.

If i take off the CButtonColumn array of the CGridView this is shown well (without the options of course).

the dataProvider is created with a CSqlDataProvider with a custimized quuery with a join with 2 tables, its possibly that result dont match with any model in the system and,this could be the error but, this can be fixed?

code the model


public static function myData($id = '' )

	{

		

		$count=Yii::app()->db->createCommand('SELECT COUNT(xx)

				FROM rr AS cs

				INNER JOIN zz AS c ON c.id = cs.rr_id

				WHERE cs.hh_id =\''.$id.'\'')->queryScalar();

		$sql='SELECT cs.id, aa, bb, cc

				FROM rr AS cs

				INNER JOIN zz AS c ON c.id = cs.rr_id

				WHERE cs.hh_id =\''.$id.'\'';

		$dataProvider=new CSqlDataProvider($sql, array(

				'totalItemCount'=>$count,

				'sort'=>array(

						'attributes'=>array(

								'id', 'aa', 'bb','cc',

						),

				),

				'pagination'=>array(

						'pageSize'=>30,

				),

		));

		return $dataProvider;

		// $dataProvider->getData() will return a list of arrays.

	}

code de view




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

    'id'=>'componentesSolicitud-grid',

    'dataProvider'=>$model->myData($model->id),

    //'filter'=>$model,

    'columns'=>array(

        'id',

        'aa',

        'bb',

        'cc',

        array('class'=>'CButtonColumn'),

    ),

));?>






thanks