Is 'id' request variable a Yii convention?

Hello boys (and girls) and happy new year!

I am new to Yii so don’t get angry with me…

I have a MySQL table as below:




Table Students{

studentid (PK)

name

age

}



managed by an Active Record model and its controller {automatically created by Gii (CRUD)}

In the view page of the Students AR (?r=students/view&id=<something>) the request variable is called “id”. Is there a convention to the name of the request variable? Why isn’t it “studentid” as the one in the table? Can I change the name of the request variables?

thanxxx

Yes you can,

The code generators are the one responsable of creating by default the $id parameter even though they are using primaryKey from table Schema. You can change that

update:

if you ID differs from conventional, please review the use of primaryKey on your model http://www.yiiframework.com/doc/api/1.1/CActiveRecord#primaryKey-detail

There really isn’t a need to prefix your table columns with the name of the table as then you end up referencing them as $student->studentid rather than just $student->id, which is much nicer. Prefixing columns with names only made sense when you were using direct SQL without ORM, so that it was easier to write queries. Generally you only need to prefix foreign keys e.g. $course->department_id. Also, I would suggest naming your tables using singular rather than plural naming (e.g. Student, instead of Students), if you have any control over creation of the database. In terms of table names either use _ as separator between words or use camelCase for your field names. This will generate the best results using Gii, without having to tweak with Gii templates.

So, my recommendation is that you change your table to make life easier for yourself when actually programming.

If you can’t then here’s a more direct answer to your question. The reason the request parameter is called “id” is because it’s just easy to call it that and that’s what gii generated. You can change it to whatever you like, just make sure you change both the variable in your action and wherever you have links that call that action.

To be even more specific change actionView($id) to actionView($studentid). Then wherever you have links like $this->createUrl(‘student/view, array(‘id’=>$model->studentid’)) change it to be $this->createUrl(‘student/view, array(‘studentid’=>$model->studentid’)). Having said that I don’t see why you’d want to change it anyway, just stick with Id and your loadModel function in the controller should already take care of things as they are.

Hey really really thanx for the immediate answers!!

@Sheldmandu:

So the name of the request variable used by a controller action is the name of the local variable parameter used in the specific action function. I hope I am not missing anything in that…

Skimming through the yii guides and tutorials, I found out about the conventional nature of Yii and how helpful it is (or how much time you have to spend to get used to it…)

I see that skimming is not enough and now I have to RTFM…

thanx again!

If that is confusing you, you can get rid of the function parameters and get the POSTed GETed parameters like this:

Yii::app()->request->getParam(‘YOURPOSTEDPARAM’);

Thanx for that too.

Sorry to bother you again with newbie stuff but it seems that the learning curve of yii is very steep.

I have a programming background in many languages including PHP/MySQL (so I have a good programming perception) but as far as PHP/MySQL concerned I have only made a few applications using databases. In these applications I followed the hard way hardcoding all the stuff a tactic which surely leads to errors and security holes. So I moved to an MVC framework and specifically to Yii due to a friend suggestion.

So…

I have already made 2 websites using Yii but they are "static" as they only have a contact form and the other pages are just pages…

Being familiar with the page encapsulation nature of an MVC framework I decided to move to the databases part of the framework.

I want to ask your opinion in what kind of books/tutorials and in what order i have to read in order to jumpstart writing databases applications.

I have the yii guide, blog guide and the book entitled agile web application development. Is there anything else I should read?

This is what I did: http://www.ramirezcobos.com/2010/12/10/yii-learning-guide/