[Solved] How Name Of The Model Is Used In The Forms

There is following fixtures:

model: User.php

controller: UserController.php

view: login.php

in the UserController there is:


<?php

class UserController extends Controller

{

    public function actionLogin()

    {

        $model = new User;

        if(isset($_POST['User']))

        {

        .

        .

        .

        }

        $this->render('login',array('model'=>$model));

}



In the login (view):


<div class="form">

    <?php echo CHtml::beginForm($action = '', $method = 'post'); ?>


    <?php echo CHtml::errorSummary($model); ?>


    <div class="row">

        <?php echo CHtml::activeLabel($model, 'username'); ?>

        <?php echo CHtml::activeTextField($model, 'username'); ?>

    </div>

    .

    .

    .

    <div class="row submit">

        <?php echo CHtml::submitButton('login'); ?>

    </div>


    <?php echo CHtml::endForm(); ?>

</div>

note: I know how to array works and other basis of programming. I just a few confused.

with use of the CVarDumper getting $_POST content as below:


CVarDumper::dump($_POST);

//Output

array ( 'User' => array ( 'username' => 'user1' 'password' => 'pass1' 'email' => '' ) 'yt0' => 'login' )

where in the login(view) ‘User’ initialized?

Do ‘User’ is a part of $model? So that $model is an instance of User model.

Do the returned ‘User’ in CVarDumper::dump($_POST); is our User model?

Yes. $model is an instance of the Class User. You have passed from controller to view.

$this->render(‘login’,array(‘model’=>$model));

Here, the key of the array is the object of the class in view. So, if you use

$this->render(‘login’,array(‘myObj’=>$model));

then your instance becomes $myObj in your view.

and in your controller $_POST[‘User’] would contain all the attribute of the model class.

My original question is here, unless we did not defined an instance of User model, so why is need our User model to use?

indeed User model how to pass to the view, we only pass $model instance on the view! below:




CVarDumper::dump($_POST);

//Output

array ( 'User' => array ( 'username' => 'user1' 'password' => 'pass1' 'email' => '' ) 'yt0' => 'login' )



Dear msoa

The name attribute for every input element is resolved by method CHtml::resolveName.

It is in the following format.




public static function resolveName($model,&$attribute)

{

............................................

............................................


 return get_class($model).'['.$attribute.']';


}



If you look at the form elements rendered by CHtml in firebug,

there is nothing surprising in what we are getting in $_POST.

Regards.

Dear seenivasan, you dont know how much help me. very thanks you.

As I understand,Yii used from parent class name of the $model(get_class in resolveName()) for beauty, and is not very necessary the parent class name of the $model. it’s right?

You could theoretically make it anything you want, but I advise against it.

This is in array notation, so that when it returns to your server, you can work with it in array notation.

For example:

Field1: User[‘username’] == $_POST[‘User’][‘username’]

Field2: User[‘password’] == $_POST[‘User’][‘password’]

If you were to change it and Not make it array notation, you might make your life a lot harder, for example your controller ( or elsewhere ) code is going to have to change to reflect it.

For example, using the default example




$model = new User();

if( isset($_POST['User']) && is_array($_POST['User']) )

 $model->attributes = $_POST['User'] // mass assignment



Without array notation you’d have to individually assign fields somehow.

So in a sense, this is function over beauty. ESPECIALLY when you have more then 1 form on the page - ex. a login form and a contact form need to be differentiated.

I understand your comments and have very thank you.

I just wanted to be sure the User model attributes in form initialization do not participate. And only the model name are used. The issue for me is resolved and now change name of this topic to a appropriate name.