Cakephp 如何在视图中显示HABTM关系的结果?

Cakephp 如何在视图中显示HABTM关系的结果?,cakephp,has-and-belongs-to-many,Cakephp,Has And Belongs To Many,我在books表和authors表之间有一个HABTM关系,它与authors\u books表连接在一起。 但是,我找不到在视图中显示字段firstname、lastname和fullname的解决方案 图书型号: class Book extends AppModel { var $name = 'Book'; var $hasAndBelongsToMany = array( 'Author' => array( 'className' => 'Auth

我在books表和authors表之间有一个HABTM关系,它与authors\u books表连接在一起。 但是,我找不到在视图中显示字段firstname、lastname和fullname的解决方案

图书型号:

class Book extends AppModel {
var $name = 'Book';
var $hasAndBelongsToMany = array(
    'Author' => array(
        'className' => 'Author',
        'joinTable' => 'authors_books',
        'foreignkey' => 'book_id',
        'associatioanForeignKey' => 'author_id'
    )
);
作者模型:

class Author extends AppModel {
var $name = 'Author';
var $virtualFields = array(
    'fullname' => "CONCAT(firstname,' ',lastname)"
);
var $fullname = 'fullname';
var $hasAndBelongsToMany = array(
    'Book' => array(
        'className' => 'Book',
        'joinTable' => 'authors_books',
        'foreignkey' => 'author_id',
        'associatioanForeignKey' => 'book_id'
    )
);
图书管理员:

   function view($id) {
    $this->Book->id = $id;
    $this->set('book', $this->Book->read());
}
my wiev.ctp中的调试($book)给出以下结果:

Array
    (
[Book] => Array
    (
        [id] => 8
        [title] => A Forest of Kings: The Untold Story of the Ancient Maya
    )

[Author] => Array
    (
        [0] => Array
            (
                [id] => 6
                [firstname] => Linda
                [lastname] => Schele
                [fullname] => Linda Schele
            )

        [1] => Array
            (
                [id] => 7
                [firstname] => David
                [lastname] => Freidel
                [fullname] => David Freidel
            )

    )
)

我相信数组中的[0][1]索引是问题所在。 我搜索了这个问题,找到了一些关于这个主题的文章,但没有找到任何可行的解决方案

非常感谢您的帮助。我使用的是CakePHP1.3.3


谢谢

您通常会在Author数组上循环,因为您可以有不同数量的Author

<ul>
    <?php foreach($book['Author'] as $author){ ?>
        <li><?php print($author['fullname']); ?></li>
    <?php } ?>
</ul>

您通常会在Author数组上循环,因为您可以有不同数量的Author

<ul>
    <?php foreach($book['Author'] as $author){ ?>
        <li><?php print($author['fullname']); ?></li>
    <?php } ?>
</ul>

您到底尝试了什么?这不应该是个问题。也许您混淆了数组的语法?您到底尝试了什么?这不应该是个问题。也许您混淆了数组的语法?