phpmysqlsqlrelational-databasenotorm

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

link

pledges

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.


Solution

  • 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");