4

After a few times adding Containable Behavior to my various model classes, I have a good mind to simply chuck the line into AppModel instead and thus make every model Containable. Which then makes me wonder, is there any situation where it is not desirable or counterproductive for a particular model to have Containable Behavior?

medcatt
  • 67
  • 7

2 Answers2

6

I would say too few to be worried about. I put containable in App Model:

class AppModel extends Model {
    public $recursive = -1;
    public $actsAs = array('Containable');

}

Containable overrides recursive anyway, so you don't really need to set recursive to -1, but I do just for clarity. Always using containable forces you into the best practice of always using only what you want/need. For small apps, it's not the end of the world to just use recursive and ignore containable, but it's still not best practice.

So, I guess the only argument for using recursive instead of containable would be that in small apps, you save yourself a tiny amount development time, and it won't really noticeably affect performance. I'd definitely go with using containable by default, and removing it where you deem it overkill, rather than the other way around.

joshua.paling
  • 13,762
  • 4
  • 45
  • 60
4

Containable can be dangerous b/c Cake acts in an extremely inefficient manner in order to get the nested results.

This site explains it well...

http://www.endyourif.com/cakephp-containable-statement-pitfalls/

Basically though the nice array you are getting back is the results of many different queries while your best performance may come from using a single query with joins.

The convenience of containable is undeniable though.

bryce
  • 566
  • 5
  • 5
  • Well, after reading the post you linked, a statement in particular came to my notice: **"After doing some research, it is clear that CakePHP will not be solving this issue in 1.2.x. I have heard rumors that it will be addressed in version 2 though, which I will be quite excited for."** Now, the question is where exactly can I read about this in official source? Because, I doubt it(concern) still exists. It'd seem very lame to perform 100s `Select`s instead of a simple `JOIN`. By the way, reference to 2.x docs and 3.x docs will be good. – Fr0zenFyr Sep 14 '15 at 06:20