Yii-user module

Hi all,

I am using yii-user module in my yii project. My problem is that i dont know how to get profile fields from an id of user.

I have order model that has an user as a foreign key. In my order view i would like to show some profile fields of the user that creates the order…

does anybody know how to achieve this?

thanks in advance,

hi,

I am still new to yii but i think that you should

import the models from the module in the config file then override the relations function in both the models

for example




//main.php

....

'import'=>array(

		'application.models.*',

		'application.components.*',

		'application.modules.user.models.*',

	),

....

//in the Order model

...

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'getUser' => array(self::BELONGS_TO, 'User', 'user_id'),

		);

	}

....

// then in the User model

...

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'orders' => array(self::HAS_MANY, 'Order', 'user_id'),

		);

	}

..



Then you can access the user details as below




$userNames=Order::model()->with('getUser')->findAll()->userName;



Hi,

thank you for your help…

actually i have the relation set. My problem is that i have order -> user -> profiles fields.

I mean, if anyone has used the yii-user module, it has profile fields where you can define dynamically your own fields. So, i have created some of them, but in code i dont know how to access them…

yii-user extension

thanks in advance,

I have it,


	public function getNombre()

	{

		$cmd = Yii::app()->db->createCommand('SELECT nombre

									FROM profiles

									WHERE user_id = :idUser')->bindValues(array(':idUser'=>$this->FKUser));

									

									

		$resultado = $cmd->queryAll();

		foreach($resultado as $row){

			$nombre = $row['nombre'];

		}

		

		return $nombre;

	}

You actually have lots of way to access it.

You can try Yii’s “findbyAttributes”, it will work perfectly with your tbl_profile table.

Hi hibernator,

So ‘Order’ BELONGS_TO ‘User’ … this is written in your Order model.

And ‘User’ HAS_ONE ‘Profile’ … this is written in User model of yii-user.

When the Profile model has an attribute named ‘phone’, then you can access it by the notation of




$order->user->profile->phone



Of course, ‘phone’ must exist as an attribute of Profile. When you have deleted or renamed it, then the code above will not work.

Well, this is a very simple solution but you might think it dirty and unstable, because the attributes of Profile can be dynamically changed. ::)

But, after all, I think the dynamic nature of yii-user’s profiles is for the developers, not for the end users. A developer can get them under his control, and he should. :)

well,

this solution is anyway better than mine :)

thanks for helping me out!