Gii CRUD alphanumeric primary key problem

If I follow the instructions to generate the model and CRUD code using Gii on a table that does not use an integer as the primary key, then clicking on the view or edit icons returns a 404.

Any ideas?

Go in the controller, in the function loadModel there is:




	$model=Richieste::model()->findByPk((int)$id);



Remove this cast to int:




	$model=Richieste::model()->findByPk($id);



And all will be fine.

" If I follow the instructions to generate the model and CRUD code using Gii on a table that does not use an integer as the primary key, then clicking on the view or edit icons returns a 404."

Where did you find these instructions ?

Old post, but that worked, so big thanks to zaccaria.

i have the same problem. i have varchar primary key. if i click the view link, the following error appears

The system is unable to find the requested action "3333-100101".

i tried to remove the (int) from my controller but with no result. i had the same error.

this is my code

in my controller





public function loadModel($id)

	{

		$model=Student::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}



my view:




<b><?php echo CHtml::encode($data->getAttributeLabel('Student_Number')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->Student_Number), array('view', 'id'=>$data->Student_Number)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('E_Child_Name')); ?>:</b>

	<?php echo CHtml::encode($data->E_Child_Name); ?>

	<br />



in the config/main:




'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

               // 'gii'=>'gii',                

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),



what is the problem and how to fix it. help plzzzzzzz

Hi freshyiiuser

I can’t see anything wrong, but I don’t think your problem lies with your primary key.

It rather looks like the wrong action somehow ends up in the url.

Check your request in Firebug. It should include the ‘view’ action.

The problem could also be a result of url manager rules.

[size="2"]Perhaps in loadModel, you could try to find the model using the Student_Number field?[/size]




public function loadModel($id)

	{

		$model=Student::model()->findByAttributes(array('Student_Number'=>$id)); <========

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}



Alternatively please try to add the following in your model (without changing controller/loadModel):





public function getId()

{

    return $this->Student_Number;

}



Regards,

Joachim

i did this too, with the same result. the same error appears again.

i checked the firebug, when i pressed the link view, the action view is not compiled at all.

the request action is broken and the error appears.

i checked with other model with the same way but the other model has an integer PK and it worked fine,

it seems that the problems is with the PK of the table student which is varchar

i added a new attribute to my table and make it as auto increment integer primary key. all the links are working fine.