Cakephp 在hasMany-through关联中检索关联模型,而无需递归

Cakephp 在hasMany-through关联中检索关联模型,而无需递归,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,我已经在两个表格之间建立了一个hasMany关联——书籍和作者。这些关联可以正常工作,除非尝试检索属于某本书的所有作者。如果我这样做 $book = $this->Book->findById($id) 返回的数组将不有一个数组;它将具有AuthorToBook连接模型信息,但不会自动获取与之关联的作者 我必须将recursive设置为2,以便检索连接模型和关联的作者。我还为每个AuthorToBook关联重新获取书籍数据 'AuthorToBook' => array(

我已经在两个表格之间建立了一个hasMany关联——书籍和作者。这些关联可以正常工作,除非尝试检索属于某本书的所有作者。如果我这样做

$book = $this->Book->findById($id)
返回的数组将有一个数组;它将具有AuthorToBook连接模型信息,但不会自动获取与之关联的作者

我必须将recursive设置为2,以便检索连接模型和关联的作者。我还为每个AuthorToBook关联重新获取书籍数据

'AuthorToBook' => array(
        (int) 0 => array(
            'id' => '1',
            'author_id' => '1',
            'book_id' => '2',
            'Author' => array(
                'id' => '1',
                'name' => 'J.R.R Tolkien',
                'date_of_birth' => '1892-01-03 00:00:00',
                'bio' => 'Professor and inventor of the Elvish Language'
            ),
            'Book' => array(
                'id' => '2',
                'title' => 'Return of the King',
                'pagecount' => '1200',
                'publisher_id' => '1'
            )
        )
    )
现在的问题是-如何在不设置递归参数的情况下获取由hasMany-Through关系形成的关联模型

这是消息来源

<?php
class Author extends AppModel
{
    public $hasMany = array('AuthorToBook');
}
?>

<?php
class AuthorToBook extends AppModel 
{
    public $useTable = 'authors_to_books';
    public $belongsTo = array('Author', 'Book');
}
?>

<?php
class Book extends AppModel {


    public $hasMany = array('AuthorToBook');

}
?>

是您的朋友,是递归的替代者。它允许您准确地指定要在每个查询中检索的关联模型


递归是一件很酷的事情,但是大家都同意,除了CakePHP的简介教程之外,不应该实际使用它。设置
public$recursive=-1代码(默认情况下会关闭),并且永不回头。

您是否正确设置了关系?即使是AuthorToBook模型中的belongsTo?你能发布你的模型代码吗?