これを回避する方法は2つあります。1つ目は、モデルで直接2番目のレベルの関連付けを定義することです。これで、どこからでもこのデータにアクセスできるようになります。このように見えるはずです.....
var $belongsTo = array(
'Foo' => array(
'className' => 'Foo', //unique name of 1st level join ( Model Name )
'foreignKey' => 'foo_id', //key to use for join
'conditions' => '',
'fields' => '',
'order' => ''
),
'Bar' => array(
'className' => 'Bar', //name of 2nd level join ( Model Name )
'foreignKey' => false,
'conditions' => array(
'Bar.id = Foo.bar_id' //id of 2nd lvl table = associated column in 1st level join
),
'fields' => '',
'order' => ''
)
);
この方法の問題は、一般的なクエリが必要以上に複雑になる可能性があることです。したがって、次のように、第2レベルのクエリを直接tefindまたはpaginateステートメントに追加することもできます。はすでに定義されています。たとえば、'Foo'が$belongsToですでに定義されている場合、以下の例のように、関連付けを機能させるには、複製の'Foo1'を作成する必要があります。)
$options['joins'] = array(
array('table' => 'foos',
'alias' => 'Foo1',
'type' => 'inner',
'conditions' => array(
'CurrentModel.foo_id = Foo1.id'
)
),
array('table' => 'bars',
'alias' => 'Bar',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array(
'Bar.id = Foo1.bar_id'
)
)
);
$options['conditions'] = array('Bar.column' => "value");
$this->paginate = $options;
$[modelname] = $this->paginate();
$this->set(compact('[modelname]'));
これが理解できるほど明確であり、誰かに役立つことを願っています。