FuelPHP:添加多个多对多关系

发布于 2025-01-02 11:06:48 字数 1469 浏览 1 评论 0原文

我有两个 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 技术交流群。

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

发布评论

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

评论(1

↘人皮目录ツ 2025-01-09 11:06:48

尝试

<?php    
$subject = Model_Subject::forge(array(
    'name' => Input::post('name'),
    'institution_id' => Input::post('institution_id'),
));

$subject->save();

foreach (Input::post('books') as $book_id) {
    $subject->books[] = Model_Book::find($book_id);
}

$subject->save();
?>

Try

<?php    
$subject = Model_Subject::forge(array(
    'name' => Input::post('name'),
    'institution_id' => Input::post('institution_id'),
));

$subject->save();

foreach (Input::post('books') as $book_id) {
    $subject->books[] = Model_Book::find($book_id);
}

$subject->save();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文