Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:43:06+00:00 2026-05-29T07:43:06+00:00

I have two MySQL tables, subjects and books, and a third table to define

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:43:07+00:00Added an answer on May 29, 2026 at 7:43 am

    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();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have two MYSQL tables: CREATE TABLE `sessions` ( `id` int(11) NOT NULL
I have two mysql tables: /* Table users */ CREATE TABLE IF NOT EXISTS
I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have two mysql tables and i want to merge the results of these
using php and mysql I have two tables, a users table and a profiles
I have two MySQL tables a and b with fields x and y. Table
I have two mySQL tables as follows: [product] table P_id | Name | Quantity
Say I have these two mysql tables: country ---- username Country John USA Sarah
I have two MySQL tables A and B . Table A has a member
I have two mysql tables that have the following structure: Table 1: ---ID---------NAME------- ---1-----

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.