Alphanumeric ID

I saw this post in relation to my issue, but it didn’t seem to solve my problem: http://www.yiiframework.com/forum/index.php/topic/12713-alphanumeric-id/page__p__62214__fromsearch__1

I have a model that uses an alphanumeric field as an ID. Yii is throwing a 400 error when I try to update an item through the Gii generated code.

Here’s my loadModel method, the id is not being typecasted to int like some forum posts mention.




public function loadModel($id)

{

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

	if($model===null)

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

	return $model;

}



I’ve tried both urlManager configurations




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






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



To no avail.

Can someone point me in the right direction?

Is your field named ‘id’?

If not you will need to override primaryKey() function in your model.

It’s not, but what’s very weird is that update does work for ID’s that are numeric only. When I add in a letter, they fail.

I did just add the primaryKey into the model, but still have the same issue.

You should reconsider using that alphanumeric key in your db. Pks should be independent, system generated values that ensure a record/row will be unique even if business rules change. This alpha numeric value should perhaps be a unique constraint instead.

As a bonus, it would eliminate the problem you are having.

I appreciate your response, but that is not an option I would like to take. I assure you the values will be unique. I belive working around the problem is not solving the problem.

Hi george3380,

I’m not very sure, but one of your other urlManager rules may catch the requests with alphanumeric ID parameters and dispatch it to a wrong route.




<module:\w+>/<controller:\w+>/<action:\w+> => <module>/<controller>/<action>, // 1st

<controller:\w+>/<action:\w+>/<id:\w+> => <controller>/<action>, // 2nd



In the example above, the 2nd rule has no chance to handle your requests with alphanumeric ID parameters.