3

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.

user1449855
  • 1,195
  • 2
  • 10
  • 14
  • 2
    Please include your associations in the respective models. – Dave Jul 05 '12 at 20:18
  • See dave's comment, Please be clearer on what you try to achive. You could give us the model definitions or table layout. You can see here that http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations using Foo.FooModel1 doesn't help. Besides that, your Model naming is not following conventions http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions. This can get you in trouble if you try to use build-in functionality – Jeroen Jul 13 '12 at 22:35
  • What is the relationship between your FooModels? Your contain array seems to suggest a hasMany/belongsTo relationship between your fooModels but your question mentions HABTM relationships. – Rob Forrest Jul 18 '12 at 15:29

1 Answers1

3

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...

Ryan
  • 116
  • 1
  • 2
  • 10