0
我已經通過兩個表(書和作者)之間的關聯建立了hasMany。除非試圖檢索屬於書籍的所有作者,否則這些關聯正常工作。如果我這樣做通過關聯檢索hasMany中的關聯模型而不遞歸
$book = $this->Book->findById($id)
數組返回將不有一個數組作者;它將擁有AuthorToBook連接模型信息,但不會自動獲取與其關聯的作者。
我必須設置遞歸爲2才能檢索連接模型和關聯的作者。我還重新獲取每個AuthorToBook關聯的Book數據。
'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通過關係形成關聯的模型沒有設置遞歸參數?
這裏的源
<?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');
}
?>
你是否設置了正確的關係?即使是AuthorToBook模型中的belongsTo?你能發佈你的模型代碼嗎? – arilia