Hi,
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){
$model=Account::model()->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
Now I have two questions, as I’m new to Yii/MVC:
-
Is it that uncommon to have non numeric primary keys that will obviously fall on that code?
-
Why is casting needed anyway? Some sort of input sanitizing? (I bet there are different locations in code in which GET params are sanitized).
-
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.
Boaz.