Problem Understanding The Flow Of Gii Crud Forms

Hello, i’m reading to the _form.php file of a view created with gii related to a model and therefore a table in my DB.

lets say that "MOD" is some model created with gii.

The model MOD gives me some rules for the fields in the table MOD.

The MODcontroller file manage the logic of the _form.php

I read that if I do:


Name: <input type="text" name="fname" />

Then to get the data from the post method it should be:


 <?php echo $_POST["fname"]; ?> 

But in the MOD/_form.php file all fields have diferent names, and MODController receive the POST message using:


 <?php $model->attributes=$_POST["MOD"]; ?> 

Why the controller doesnt call $_post["x"] for each of the values from the model and can use a general name to

get the values???

I’m trying to replicate this in my own controller and view, but I can only receive the post data using exactly the names of each fields …

Please help me understand this =)

Part of this is a feature of PHP. If your form is like:




First Name: <input type="text" name="Person[FirstName]" />

Last Name: <input type="text" name="Person[LastName]" />

Age: <input type="text" name="Person[Age]" />



Then PHP will create a $_POST item called "Person" that is an associative array like so:




<?php print_r($_POST["Person"]); ?>



prints:




Array

(

    [FirstName] => 'Jane'

    [LastName] => 'Smith'

    [Age] => 22

)



You can access an individual element like:


<?php echo $_POST["Person"]["FirstName"]; ?>

This is nothing special about Yii it’s just how PHP works.

As for the Yii part, look at the rendered html source of your forms. When you do


<?php echo $form->textField($model,'firstName',array('size'=>60,'maxlength'=>200)); ?>

It won’t generate a html like:


First Name: <input type="text" name="FirstName" />

instead it will create html like:


First Name: <input type="text" name="Person[FirstName]" />

The final piece of the puzzle is this line:


 $model->attributes = $_POST["Person"]; 

This is called massive assignment. Basically Yii is assigning each attribute of your model that has a corresponding value in the $_POST["Person"] array. You can read more about it here: Understanding "Safe" Validation Rules

You are probably talking about the input elements generated by CHtml or CActiveForm.

These classes can generate "active" input elements, which use information from your model to create and name the input elements.

The CModel::setAttributes method understands how these elements are named and how to correlate them to the corresponding model attributes.

This is a very superficial explanation, but should get you started.

Please post again if you need more clarifications.

You can also find more information in the guide.

Thank you so much for your quick reply! =)

Now that I got a better understanding I don’t know why this isn’t working:

This code sends a POST[‘MOD’] array, but the different radiobuttonlist are considered like only one, and can’t select differents options. (I’m following this example)




foreach($preguntas as  $i=>$question)

{

    $accountStatus = array('1'=>$question->alt1,'2'=>$question->alt2);

    echo CHtml::activeRadioButtonList($model,'[$i]alt',$accountStatus);

}



But in this code the different radiobuttonlist are considered like differents, so I can select several options,

the problem is that in the controller I don’t get a $_Post[‘MOD’], instead I get some $_Post[‘altX’] (X:number)




foreach($preguntas as  $i=>$question)

{

    $accountStatus = array('1'=>$question->alt1,'2'=>$question->alt2);

    echo CHtml::activeRadioButtonList($model,'alt',$accountStatus,

    			array('name'=>'alt'.$question->_id'));

}



Maybe you can help me… I can’t see what I’m doing wrong and i’m pretty new to php so i’m trying to learn all properties but probably there’s some that I don’t know that can help me to achieve my goal.

Thanks again for your help!

Turns out that the first aproach was enought but in there’s a error in the tutorial that I didn’t see. It should be as the following code.




foreach($preguntas as  $i=>$question)

{

    $accountStatus = array('1'=>$question->alt1,'2'=>$question->alt2);

    echo CHtml::activeRadioButtonList($model,'['.$i.']alt',$accountStatus);

}



Thanks again for your accurate and quick support.

Actually I don’t think there was an error in the tutorial. What you experienced is the difference between single and double quotes. In PHP variables inside double quotes will get expanded. So:


echo CHtml::activeRadioButtonList($model,"[$i]alt",$accountStatus);

will give you the same result as:


echo CHtml::activeRadioButtonList($model,'['.$i.']alt',$accountStatus);

However the following, which uses single quotes, will not (because $i will not be expanded):


echo CHtml::activeRadioButtonList($model,'[$i]alt',$accountStatus);

The tutorial you referenced does correctly use double quotes.

I didn’t know that, thanks for the correction