default Gii code assumes numeric (int) primary keys


I just found out why the freshly generated CRUD code by Gii on my DB doesn’t work. While list/index action works fine but showing single row (view action) didn’t work - 404.

It occurrs since in the controller generated by Gii when it tries to load the model using ActiveRecord, it uses the following function:


 * Returns the data model based on the primary key given in the GET variable.

 * If the data model is not found, an HTTP exception will be raised.

 * @param integer the ID of the model to be loaded


public function loadModel($id){



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

	return $model;


Now I have two questions, as I’m new to Yii/MVC:

  1. Is it that uncommon to have non numeric primary keys that will obviously fall on that code?

  2. Why is casting needed anyway? Some sort of input sanitizing? (I bet there are different locations in code in which GET params are sanitized).

  3. This at least deserves a comment in code, tutorial, whatever. I’m contemplating on how to approach this. Suggestions based on Yii ecosystem know-about are welcomed.


I’ve removed this (int) conversion from Yii SVN trunk a while ago. So in the next release version it will come w/o it.

  1. Yes, that is not a common practice.

You just need to remove the cast and it will work well.

Or you can use giix. giix 1.6 allows you to use non-numeric PKs (and also allows you to use any other attribute besides the PK).

since the cast is in "my" controller, I saw no methodological problem in customizing it by removing that cast operation, and voila, it obviously worked :slight_smile:

thanks for the offer though.