0

I'm having trouble wording my problem, so it's been tough to search for an answer. Hopefully you'll know how to help.

I am creating a CakePHP 2.1 Plugin that will interact with a series of its own Models: - Friend - Group - User

Friend and Group are models that are created specifically for the Plugin, and they function within the plugin normally. However, the User model is really just an alias for some other table in the parent app.

So, if "My Awesome Application" decides to use "My Awesome Plugin", it will have to have its own "users" table (though it may called something else). Let's say "My Awesome Application" has a Model called MyUser. "My Awesome Plugin" wants to dynamically tell its internal User model to $useTable = "my_users".

My question is, how do I pass that data to the Plugin? How do I configure "My Awesome Plugin" to understand that User should $useTable "my_users";

tereško
  • 58,060
  • 25
  • 98
  • 150
Dustin
  • 846
  • 8
  • 13

2 Answers2

2

As I understand you would like a Model in a PlugIn to use a table that would typically belong to a Model in your Application - by the conventions. Have you tried statically setting:

public $useTable = "my_users";

in the plugin? All plugins usually get initialized when Cake starts up, so all configurations should be loaded then. Why do you need this - it does really restrict you a lot? Will the table being used by the Plugin model change runtime?

The Model class also has some goodies you may find useful:

$this->User->table;

holds the table name for the model - the table that is currently being used that is**.

Also you can set the source table for the Model (inside a Controller) with:

$this->User->setSource('table_name);

** I am not sure if this applies when you use Model::setSource(). It would be interesting to check out what $this->User->table; holds after a Model::setSource() call.

Borislav Sabev
  • 4,776
  • 1
  • 24
  • 30
  • Thanks for this. I eventually found the Model->setSource() functionality, and by combining it with my other answer (below), I was able to get it working. The issue was how to set the table for the Model that was within the plugin from outside of the plugin. Thanks for the help. – Dustin Jun 23 '12 at 18:40
0

I've figured out a way to accomplish this, but it might not work in all scenarios for all people.

I created a Component in my Plugin, and then I call the Component in my Controller. I pass the name of the users Model through the Component. This way, I can get information about the users Model, and I can set it as the useTable to my Plugin for use in the Plugin.

Of course, this method restricts me to using the Component to utilize the Plugin, but that's probably for the best.

Here's an example of how I did it:

// in the AppController
public $components = array(
'MyPlugin.MyPluginComponent' => array('userModel'=>'UserModelName')
);

// in the Plugin's Component
class MyPluginComponent extends Component {

function initialize($controller) {

    //get the base user model
    $this->UserModel = ClassRegistry::init($this->settings['userModel']);

    //set the useTable for our plugin's user model
    $this->PluginUser = ClassRegistry::init('MyPlugin.PluginUser');
    //set the useTable value for this model
    $this->PleaseUser->setSource($this->UserModel->useTable);
}

That seems to work for me. Hope this helps someone else.

Dustin
  • 846
  • 8
  • 13