如何在视图中显示 HABTM 关系的结果?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通常会循环访问 Author 数组,因为您可以拥有不同数量的作者。
You would normally loop over the Author array, since you can have a varying number of authors.