2
votes

The CakePHP 2.0 documentation says the following:

In CakePHP some associations (belongsTo and hasOne) performs automatic joins to retrieve data, so you can issue queries to retrieve models based on data in the related one.

But this is not the case with hasMany and hasAndBelongsToMany associations. Here is where forcing joins comes to the rescue. You only have to define the necessary joins to combine tables and get the desired results for your query.

But I'm seeing different behavior in two ways:

  1. All my hasAndBelongsToMany associations are joined automatically (unless I tell them not to using $options['contain']=... when I search.

  2. In one situation, I CAN'T get a hasMany association to appear unless I force it with $options['joins']=...

The latter problem may be something I've done wrong, but it's difficult to troubleshoot without knowing how it SHOULD work. I suspect that the documentation is either inaccurate on this point, or perhaps it works completely differently when the containable component is in use. (NOTE: We have public $actsAs = array('Containable'); in our AppModel so all models inherit it.)

1

1 Answers

0
votes

Try to define containable behavior at top of every model use has many or HABBTM, like

class MyModel extends AppModel {
    public $actsAs = array('Containable');
..................

You can use also use $options['contain']=... which fetch related data auto magically.

Also you can set value of recussrisive to fetch associated data like

$this->MyModel->recursive = 1;