user1449855
user1449855

Reputation: 1365

CakePHP plugin models not associating when using Containable

I have a CakePHP plugin named Foo that has a component, no controller, and several joined models. It's structured like this:

/Plugin/Foo/Model/
 -FooModel1.php
 -FooModel2.php
 -FooModel3.php
 -FooModel4.php

In order to connect with the standard (non-plugin) code, one of the plugin models is conditionally associated with a standard model called Bar. I don't believe this is relevant to the problem, but I want to be thorough.

In FooController I make use of Containable, several layers deep:

$params = array(
  'conditions' => array('Bar.id' => $bar_id),
  'contain' => array(
    'FooModel1' => array(
      'FooModel2' => array(
        'FooModel3' => array('Something', 'FooModel4')
      ),
    ),
  ),
);

This creates an error like:

Warning: Model "FooModel2" is not associated with model "FooModel3" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 339]

So in my plugin controller I need to contain plugin models. I've tried prefixing the models with Foo (e.g. Foo.FooModel1) but Containable thinks I'm trying to get a model named Foo. In the $hasAndBelongsToMany array in the plugin models I've tried both 'Model1' => array() and 'Foo.Model1' => array(), neither of which work. How do I correctly set up this association?

Side note: when I move the models out of the plugin and into the standard /Model/ directory everything works fine. The problem seems to come from incorrect associations rather than the logic of the code itself.

Thanks a lot for the help.

Upvotes: 3

Views: 1155

Answers (1)

Ryan
Ryan

Reputation: 116

When setting up your associations in your models, make sure to prefix plugin models with their plugin name...otherwise cake will generate one on the fly...and not use the file in your plugin...

Upvotes: 3

Related Questions