Get Customized Array

hello everyone!!

I have following two tables

user

1)id

2)name

user_details

1)id

2)user_id(FK reference table user)

3)city

$user = User::model()->with(‘user_details’)->findByPk(Yii::app()->user->id);

I want a resultant array containing ‘id’ and ‘city’

how can i fetch city using CHtml::listData() ??

I wrote CHtml::listData(User::model()->with(‘user_details’)->findByPk(Yii::app()->user->id),‘id’,‘city’);

But the ‘city’ field was blank.

Can we fetch the data from the related record using CHtml::listData() ??

Thanks,

Mohit

Even if you write it correctly, it’s only going to return one record. What are you intending to use it for?

I just want to make a key value pair array where key will be the id from the user table and value will be the city from user_details table.

Lets say if i have a record in user table

id|name

1 |Mohit

and in user_details table

id|user_id|city

1 |1 |chandigarh

i have fetch the record using this statement

$user = User::model()->with(‘user_details’)->findByPk(Yii::app()->user->id);

Now can CHtml::listData() function provide me the shortcut for a customized array like this

array(

1=>chandigarh

)

What are you going to use the array for though? From your specification, the following would work fine:




$user = User::model()->with('user_details')->findByPk(Yii::app()->user->id);


$cityArray = array($user->id => $user->user_details->city);



I can’t see any reason to do that though.