Represent Complex Attributes In Yii Model.

Hi all,

I want to define a complex attribute in yii model.Say for example student has a complex attribute which contains f_name,m_name,l_name.How can i represent it in yii model?

try this


public $f_name;

If it’s just a read-only attribute, just add a get* method.


public function getFullName()

{

  return $this->f_name . ' ' . $this->m_name . ' ' . $this->l_name;

}


echo $model->fullName;

No i want to store it in the db also.

First of all, you can store f_name, m_name and l_name as table columns.

But if for some reason this is not the case, you can do whatever you want in beforeSave() and afterFind().

For example:

public $f_name;

public $l_name;

public $m_name;

in beforeSave: $this->name = $this->f_name . ‘|’ . $this->l_name . $this->m_name;

in afterFind: list($this->f_name, $this->l_name, $this->m_name) = explode(’|’, $this->name);

Do not forget to add custom attributes to validation rules, for mass assignment.

if you want to store a value on db you can create the object and save the record…

e.g


$model = new User();

$model->f_name='your firstname';

$model->save(false);



I have a xsd file with structure,

XSD file with structure…

<StudentSheet>

<simpleProperty>

&lt;name&gt;student_id&lt;/name&gt;


&lt;datatype&gt;long&lt;/datatype&gt;

</simpleProperty>

<simpleProperty>

&lt;name&gt;email&lt;/name&gt;


&lt;datatype&gt;string&lt;/datatype&gt;

</simpleProperty>

<complexProperty>

&lt;name&gt;Details&lt;/name&gt;


&lt;datatype&gt;array&lt;/datatype&gt;


&lt;itemDefinition&gt;


  &lt;simpleProperty&gt;


    &lt;name&gt;f_name&lt;/name&gt;


    &lt;datatype&gt;string&lt;/datatype&gt;


  &lt;/simpleProperty&gt;





  &lt;simpleProperty&gt;


    &lt;name&gt;m_name&lt;/name&gt;


    &lt;datatype&gt;string&lt;/datatype&gt;


  &lt;/simpleProperty&gt;





  &lt;simpleProperty&gt;


    &lt;name&gt;l_name&lt;/name&gt;


    &lt;datatype&gt;string&lt;/datatype&gt;


  &lt;/simpleProperty&gt;





  &lt;/itemDefinition&gt;

</complexProperty>

</StudentSheet>

how can i represent it in a yii model?

please post your final question properly…not one by one.

How can i represent this complex attribute in yii model?