Combine Two Models Objects In One Cdetailview

Dear all,

I have a model Adress and a model Person. Each person has an adress now I want to display the person and adress data in one CDetailView.

What I’ve tried is to use array_merge($person->attributes, $person->adress->attributes) but then I get the problem that one attribute of person is salutation which is stored with 0 => Mr. and 1 => Mrs. in the database. Therefore I’ve implemented a getSalutation-method which converts this database value to a readalbe value.

The $model->attributes oder $model->getAttributes() don’t call this special getSalutation() so what can I do?

Thanks!

Hi Gerrit, welcome to the forum.

Hmm, why do you have to merge $person->attributes and $person->address->attributes?

I’ve never done that when using CDetailView.

your question is not clear , can you share some code, do u have the relationship b/w person and address if so, then you can solve your problem using relationship http://www.yiiframework.com/doc/guide/1.1/en/database.arr. But Please Clarify your questions.

and whey your are showing data in CDetailView b/c i assume there may be one-many relationship! one person have many addresses!!

Thank you, yes there is a relationship so I could update my CDetailView-config to show the field adress.street. Now it works.

But there is an additional question: I’ve implemented a method to translate the salutation to a readable value in $person->getSalutation();

How can I use the getSalutation-method in CDetailView-config?




<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'salutation', // <-- How to use getSalutation here?

		'forname',

		'surname',

		'adress.street',

		'adress.zip',

		'adress.city',

		'created_at',

		'updated_at',

	),

)); ?>



define a method for it in your model and return its readible value something like:

array

(

        'name'=&gt;'salutation',


            'value'=&gt;MyModel::getMyValue(&#036;model-&gt;salutation),


),