Extending a module's model

I’m surprised that I can’t find what I’m looking for already answered, but…

New to PHP and Yii…from a .Net background. Anyway, I’m building a Yii application and using a Yii user extension module which is pretty good, but I want to integrate that model into my existing model. For example, a user has addresses, can be a student or a teacher, etc. So I want to take the “user” that is in the module, and extend it with my own model class which has the additional relationships. Is this possible?

I’ve tried just extending the user like this:

[i]class MyUser extends \application\modules\user\models\User

{

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(


		'addresses' => array(self::HAS_MANY, 'Address', 'user_id'),


		'coaches' => array(self::HAS_MANY, 'Coach', 'user_id'),


		'students' => array(self::HAS_MANY, 'Student', 'user_id'),


	);


}

[/i]

This doesn’t appear to be working. When I try to access the addresses in my AddressController like this:

[i]

    	public function actionIndex()


{


            $user =  MyUser::model()->findByPk(Yii::app()->user->id);


            $model = $user->addresses; 


	$this->render('admin',array(


		'model'=>$model,


	));


}

[/i]

the code just goes off into never-never land and I don’t get anything back.

Is what I’m trying to do possible? Does anyone have any examples of this?

Shawn

Hi Shawn, welcome to Yii. :)

UserModule class of yii-user has a property named ‘relations’ where you can establish the relations to your own models without tweaking the yii-user code.




// protected/config/main.php

'modules'=>array(

	'user' => array(			// yii-user

		'tableUsers' => 'users',

		'tableProfiles' => 'user_profiles',

		'tableProfileFields' => 'user_profile_fields',

		'relations' => array(

			'addresses' => array(self::HAS_MANY, 'Address', 'user_id'),

			'coaches' => array(self::HAS_MANY, 'Coach', 'user_id'),

			'students' => array(self::HAS_MANY, 'Student', 'user_id'),

		),

	),

	...



A HAS_MANY relation returns an array of objects.

If your “admin” view is like a standard one that gii generates, then it’s no wonder that it will crash because it requires a single object. :)

Try




<?php


Yii::import('application.modules.user.models.*');


Class UserExt extends User

{

	

	

}



Happy coding.