Form issue

Hi all.

I have model with many relationships. One of them is belongs_to.

So, in the form creation i do this.

<div class="simple">

&lt;?php echo CHtml::activeLabelEx(&#036;dadosPessoais-&gt;logradouro, 'Endereco') ?&gt;

&lt;?php echo CHtml::activeTextField(&#036;dadosPessoais-&gt;logradouro, 'Endereco', array('maxlength' =&gt; 100)) ?&gt;


And the created form is:

<div class="simple">

&lt;label for=&quot;Logradouro_Endereco&quot;&gt;Endereço&lt;/label&gt;

&lt;input maxlength=&quot;100&quot; name=&quot;[color=&quot;#FF0000&quot;]Logradouro[Endereco][/color]&quot; id=&quot;Logradouro_Endereco&quot; type=&quot;text&quot; value=&quot;Rua Sem Saída.&quot; /&gt;


The input name is: Logradouro[Endereco]. But it should be: DadosPessoais[Logradouro][Endereco]

Is this possible or Yii don’t handle this?

With this approach the Post variable will be logical grouped.

unfortunately, Yii doesn’t handle this.

Take a look at this topic in the guide:

Maybe you can do something like this, but looping through the relations array.

This happens cause Yii uses the class name of the model to create the id and name of input fields.

Thank for the answear!

Well, tabular input is not a solution for me in this case =|

I’ll try another approach doing it manually.

I found another problem with this same example:

<div class="simple">

<?php echo CHtml::activeLabelEx($dadosPessoais->logradouro, ‘Endereco’) ?>

<?php echo CHtml::activeTextField($dadosPessoais->logradouro, ‘Endereco’, array(‘maxlength’ => 100)) ?>


And the created form is:

<div class="simple">

<label for="Logradouro_Endereco">Endereço</label>

<input maxlength="100" name="Logradouro[Endereco]" id="Logradouro_Endereco" type="text" value="Rua Sem Saída." />


I got this error:



Property "DadosPessoais.logradouro" is not defined.

Source File

C:\SubVersion\yii framework\framework\db\ar\CActiveRecord.php(430)

BUT… I know the problem, i can’t solve it.

[color="#8B0000"]$dadosPessoais->logradouro[/color] Doesn’t exist yet because was create with $dadosPessoais->new DadosPessoais();

But when I got it with $dadosPessoais = DadosPessoais::model()->with(/****/)->find( /*****/ ) it works perfecly except but the name field issue (which I can bypass this).

So… How can I access a BELONGS_TO variable for a newly created model?

It doesn’t matter if it does not exists, it will be blank anyway, But this error is annoying…

I think Yii should provide this.

I found a temporary "solution" for the problem.

In the __contruct() method of my model I can inject the field only when they are created.

public function __construct($attribute = array(), $scenario='')


	if ($this->getIsNewRecord())


		$this->logradouro = new Logradouro;

		$this->telefones[0] = new Telefone;

		$this->telefones[1] = new Telefone;

		$this->telefones[2] = new Telefone;

		$this->tituloEleitor = new TituloEleitor;

		$this->dados_comerciais = new DadosComerciais;


	parent::__construct($attribute, $scenario);


The reason for the ‘if’ statement is because it overwrites this find() method:

$dadosPessoais = DadosPessoais::model()->with(

	array('dados_comerciais', 'estadoCivil', 'grauInstrucao', 'logradouro', 'tituloEleitor', 'telefones')