super_kamil
super_kamil

Reputation: 393

Module dependency and relations

"Some commonly used features, such as user management, comment management, may be developed in terms of modules so that they can be reused easily in future projects." - http://www.yiiframework.com/doc/guide/1.1/en/basics.module

I have a lot of projects that requires a user. Every time quite the same database structure and features. Registration, login, logout etc. Yii tells me that i can reuse modules. Cool... let's start:

I have 3 parts: User, Campaign and Website.

In this project the CampaignModule has a relation to the UserModule (campaign_user [user_id, campaign_id])

The WebsiteModule has a relation to the CampaignModule and to the UserModule.

I want to reuse the UserModule in other projects with features like registration, login, edit etc.

Actual situation: After creating models with gii every one has relations and dependencies across the modules. e.g.

UserModule: 'campaigns' => array(self::HAS_MANY, 'Campaign', 'user_id'),

To use the WebsiteModule it's necessary to include the User- and CampaignModule. Now i even have to include Website- and CampaignModule to use the UserModule!

I also want to update the UserModule across many projects and maybe build a framework with some basic modules.

What is the right way to plan an architecture like this?

Upvotes: 0

Views: 623

Answers (1)

Asgaroth
Asgaroth

Reputation: 4334

There is a yii-user module, what they do, is they allow you to specify additional relations for the User model, on the module configuration:

/**
 * @return array relational rules.
 */
public function relations()
{
    $relations = Yii::app()->getModule('user')->relations;
    if (!isset($relations['profile']))
        $relations['profile'] = array(self::HAS_ONE, 'Profile', 'user_id');
    return $relations;
}

So you can do something like:

'modules'=>array(
    'user' => array(
            ...
            'relations' => array(
              'categories' => array(CActiveRecord::HAS_MANY, 'Category', "user_id"),
              'account' => array(CActiveRecord::HAS_ONE, 'Account', "user_id"),
            ),
            ...
        ),
  ),

Upvotes: 1

Related Questions