FuelPHP:添加多个多对多关系
我有两个 MySQL 表、主题和书籍,以及第三个表来定义这两个表之间的多对多关系:
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`isbn13` char(13) NOT NULL,
`isbn10` char(10) NOT NULL,
`author` varchar(255) NOT NULL,
`language` varchar(100) NOT NULL,
`edition` smallint(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `books_subjects` (
`subject_id` int(11) NOT NULL,
`book_id` int(11) NOT NULL,
PRIMARY KEY (`subject_id`,`book_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `subjects` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`institution_id` mediumint(9) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
要添加新主题,我使用 ORM 包:
<?php
$subject = Model_Subject::forge(array(
'name' => Input::post('name'),
'institution_id' => Input::post('institution_id'),
));
$subject->save();
?>
我还将多对多关系添加到主题模型:
protected static $_many_many = array('books');
检索主题及其分配的书籍工作正常(我已手动向 books_subjects 表添加了一些记录),但我不知道如何使用 ORM 包添加关系。 这些关系位于这样的数组中(通过使用 Input::post() 检索):
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
对于有一定 FuelPHP 经验的人来说,这应该很容易,但我似乎无法弄清楚......
提前感谢您的帮助我出去!
I have two MySQL tables, subjects and books, and a third table to define many-to-many relationships between these two:
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`isbn13` char(13) NOT NULL,
`isbn10` char(10) NOT NULL,
`author` varchar(255) NOT NULL,
`language` varchar(100) NOT NULL,
`edition` smallint(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `books_subjects` (
`subject_id` int(11) NOT NULL,
`book_id` int(11) NOT NULL,
PRIMARY KEY (`subject_id`,`book_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `subjects` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`institution_id` mediumint(9) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
To add a new subject, I use the ORM package:
<?php
$subject = Model_Subject::forge(array(
'name' => Input::post('name'),
'institution_id' => Input::post('institution_id'),
));
$subject->save();
?>
I have also added the many-to-many relationship to the Subject model:
protected static $_many_many = array('books');
Retrieving subjects with their assigned books works fine (I have added a few records to the books_subjects table manually), but I don't know how to add the relationships using the ORM package.
The relationships are in an array like this (retrieved by using Input::post()):
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
For someone with some experience with FuelPHP this should be pretty easy, but I just cant seem to figure it out...
Thanks in advance for helping me out!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try