简体   繁体   中英

How do I JOIN many-to-many tables using NotORM

I'm learning NotORM to produce a simple system for school. I want to be able to award 'Pledges' to 'students'. Here is my data structure:

My tables:

students

  • -studentid (PK) -firstname -surname -dateofbirth -vmg -yeargroup

link

  • -linkid (PK) -studentid (FK) -pledgeid (FK) -timeofaward

pledges

  • -pledgeid (PK) -pledgename -pledgeinfo

The code from a great NotORM tutorial ( http://www.sitepoint.com/database-interaction-made-easy-with-notorm/ ) says that I should do this:

<?php
foreach ($books as $book) {
echo "<tr>";
echo "<td>" . $book["title"] . "</td>";
echo "<td>" . $book["author"] . "</td>";
// book_category table joins book and category
$categories = array();
foreach ($book->book_category() as $book_category) {
    $categories[] = $book_category->category["category"];
}
echo "<td>" . join(", ", $categories) . "</td>";
echo "</tr>";
}
?>
  1. Is my data structure correct for using NotORM
  2. How do I translate the example so that it shows the students and which awards have been awarded to them. - I feel like I have tried every variation of that code and still can't get it to work.

Many thanks in advance.

The key to your problem: starting from the Relation-Table (here Link) and relaying on NotORM to join the other tables.

See how simple the code may be:

$db->link("pledge.name", $someName)->select("student.firstname, student.lastname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM