using zii and the data passed ( from ID to value )

Let’s take an example:




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

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'todoId',

		'userId',

		'categoryId',

		'title',

		'desc',

		'end',

		array(

			'class'=>'CButtonColumn',

		),

	),

));



how can I pass to zii not the todoId but the name of the todo? how can i pass to zii the name of the category referenced by the categoryId?

Is there a way to attach to that model attribute a behaviour to use a customized tostring?

Are you trying to use a value from another table?

try this approach instead of ‘reference_field_id’ use ‘table_name.field_name’

hope this helps

doodle

I don’t understand sorry. The model i’m passing to the zii extension has some normal attribute and some attribute that are foreign key like categoryId or userId or todoId. Insted of passing these id to zii I want to pass the categoryName (based on categoryId), userName ( based on userId ) and todoTitle (based on todoId). How can I pass them the zii extension if these are not model’s attributes?

[size="5"]Anoter problem with zii.widgets.grid.CGridView. This is the code:[/size]




$dataProvider=new CActiveDataProvider('Todo', array(

					    'criteria'=>array(

					        'condition'=>'categoryId=:catId AND userId=:userId',

					        'params'=>array(':catId'=>$cat->categoryId, 'userId'=>$user->userId),

					    ),

					    'pagination'=>array(

					        'pageSize'=>2,

					    ),

					));

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

						'dataProvider'=>$dataProvider,

						'columns'=>array(

							'categoryId',

							'title',

							'desc',

							'end',

							'priorityId',

							'privacy',

							'url',

							'location',

							'address',

							array(

								'class'=>'CButtonColumn',

							),

						),

					));



They create 3 buttons: view, update and delete. The problem is for the delete button. When I click it, it prompt the js dialog, I say yes and then it raise a 400 exception saing: Invalid request. Please do not repeat this request again.

This mean that Yii doesn’t make a Post request. This is the code in the controller:




public function actionDelete()

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$t = Todo::model()->findByPk($_GET['id']);

			$t->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_POST['ajax']))

				$this->redirect(array('index'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again. its not post');

	}



How can I solve it?

Are the relations in the model setup?


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

                                                'dataProvider'=>$dataProvider,

                                                'columns'=>array(

                                                        'foreigntable.fieldname', <------

                                                        'title',

                                                        'desc',

                                                        'end',

                                                        'foreigntable.fieldname', <------

                                                        'privacy',

                                                        'url',

                                                        'location',

                                                        'address',

                                                        array(

                                                                'class'=>'CButtonColumn',

                                                        ),

                                                ),

                                        )

This should solve the first problem

doodle

yes, I’m going to try and give you a feedback :)

EDIT: it works pretty well, now I only need to handle this zii extension :)

I need to change the color of the font and the column labels :)

Update: This seems to be a bug in yii svn as of 2/3/2010

Hi I tried this but I’m getting an exception

  • NetWSwitch is primary

  • facility is the relation name to foreign table SA_FACILITY, REGION is the column i’m trying to display.

When adding foreigntable.column for example ‘facility.REGION’, get an exception:

any help is appreciated…