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

发布于 2024-09-18 09:47:37 字数 1835 浏览 5 评论 0原文

我在 books 表和authors 表之间有一个HABTM 关系,并与authors_books 表连接。 但是,我找不到在我的视图中显示名字、姓氏和全名字段的解决方案。

book-model:

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

author-model:

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'
    )
);

books_controller:

   function view($id) {
    $this->Book->id = $id;
    $this->set('book', $this->Book->read());
}

debug($book) 在我的 wiev.ctp 中给出以下结果

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] 索引是问题所在。 我搜索了该问题,并找到了一些有关该主题的文章,但无法找到任何可行的解决方案。

非常感谢任何帮助。我正在使用 cakephp 1.3.3

谢谢

I have a HABTM relationship between a books table and an authors table, joined with an authors_books table.
However, I can't find a solution to display the fields firstname, lastname and fullname in my view.

book-model:

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

author-model:

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'
    )
);

books_controller:

   function view($id) {
    $this->Book->id = $id;
    $this->set('book', $this->Book->read());
}

debug($book) in my wiev.ctp give these results:

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
            )

    )

)

I belive the [0][1] index in the array are the problem.
I've searched the problem, and found some articles on the subject, but can't get any of the solutions to work.

Any help is greatly appreciated. I am using cakephp 1.3.3

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-09-25 09:47:37

您通常会循环访问 Author 数组,因为您可以拥有不同数量的作者。

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

You would normally loop over the Author array, since you can have a varying number of authors.

<ul>
    <?php foreach($book['Author'] as $author){ ?>
        <li><?php print($author['fullname']); ?></li>
    <?php } ?>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文