0
votes

I have several projects in CakePHP, and would like to move common code into plugins and use seperate GIT repositories for those.

For example, I created a UserManager plugin which contains MVC for users, groups and permissions.

My problem is: the different projects have different (additional) relations to the models from the plugin. E.g., one project should have "User belongsTo Location" in addition.

I'm now confused how to set this up properly. The manual tells how to override Plugin views, but not how this is done with models and controllers.

How can this be done in a clean way?

1
Please don't forget to mention your exact CakePHP version.ndm

1 Answers

0
votes

You can simply extend the plugin classes and override/add the necessary associations, just like you're probably already doing it with AppModel respectively UserManagerAppModel.

http://book.cakephp.org/2.0/en/plugins.html#plugin-models

Here's a basic example (assuming the user class in the plugin is named User):

App::uses('User', 'UserManager.Model');    

class AppUser extends User
{
    public $belongsTo = array('Location');
}

Or create the associations dynamically in case there are existing ones that need to be kept:

class AppUser extends User
{
    public function __construct($id = false, $table = null, $ds = null)
    {
        parent::__construct($id, $table, $ds);

        $this->bindModel(array('belongsTo' => array('Location')));
    }
}