About Yii Blog Tutorial

Hi,

I am following the Yii Blog Tutorial and i have few questions about the form used for the user creation.

This form use a CActiveForm widget. In the following line :


$form->textField($model,'username',array('size'=>60,'maxlength'=>128));

  1. if i replace “username” with “anotherfield” i get a “Property ‘User.anotherfield’ is not defined” error. If i create a “anotherfield” column in tbl_user there is no error. Does this mean that the DB is checked for each field existence of the form each time i use this widget ?

  2. for the DB column "username" the form field id is "User_username" and the name is "User[username]". How can i change this without changing the DB column name ?

Thanks.

  1. Can be said like that…

  2. why would you want to change it. The name of the input will be always “modelName[fieldName]” and its id will always be “modelName_fieldName”. If you change it, then it won’t worked in your controller code where there are line like this


$model->attributes = $_POST['modelName'];

because you are putting other name. So you might need to manually write it down to like this




$model->attributes = $_POST['modelName'];

$model->username = $_POST['modelName']['otherFieldname'];



But as you can see, it only make your code longer…

Thank you for your reply.

For 1) is it possible to avoid it or we should not do that ?

For 2) i dont like to name form fields the same as DB tables that’s why. Is there any way to map field names to tables names like the attributeLabels method does for form labels ? The attributeNames method does not seem to do that.

I don’t think currently there is function for mapping table field name to class property (I am not sure though)

but you can write one I think. You can just write getter (or magic function) yourselves, maybe like this


//in model

public function getOtherFieldName(){

  return $this->username;

}



I will search how to implement this. Thank you.