Welcome Guest, Not a member yet? Register   Sign In
Datamapper ORM Multi-tabel Relationship Halp!
#1

[eluser]bcdennis[/eluser]
From the doc:

Quote:Occasionally however, you might have a need to define a relationship between more than two tables. In this case, you will have to create a joined table with more than two foreign keys, one to each of the tables involved in the relationship. In this situation Datamapper can no longer automatically generate the name of the joined table. Instead, you will have to use the join_table to manually define the name of the joined table.

I'm looking for how to configure and use the datamapper for a multi relationship like this (just an example, please ignore any flaws in the data design).

A book has many chapters. A chapter belongs to one book. Each chapter has many pages. A page can be in multiple chapters. Assume each is it's own table. How do set the join table up, and how do I setup the has_manys and has_one's for each class?

I'm guessing the join table would look like this:
Code:
books_chapters_pages
*******************
[id][book_id][chapter_id][page_id]

I'm guessing the has_many's would look like this:
Code:
class Book ...
{
   var $has_many = array(
       "chapter" => array("join_table" => "books_chapters_pages"));

...

class Chapter ...
{
   var $has_one = array(
       "book" => array("join_table" => "books_chapters_pages"));
   var $has_many = array(
       "page" => array("join_table" => "books_chapters_pages"));

...

class Page ...
{
   var $has_many = array(
       "chapter" => array("join_table" => "books_chapters_pages"));

...

Expected usage:
Code:
$book = new Book();
$book->get_by_title("Sample Book 1");

$book->chapter->get_by_title("Chapter 1");

$book->chapter->page->get();

And the pages retrieved are what belongs to that chapter which belongs to that book.

That doesn't work. Has anyone done multi-table relationships that can show me how to properly setup the tables, the relationships and give me some examples of usage?

Thanks!


Messages In This Thread
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 07-27-2011, 12:34 PM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 07-28-2011, 02:04 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-04-2011, 07:18 PM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-05-2011, 04:49 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-05-2011, 08:54 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-05-2011, 11:37 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-06-2011, 03:44 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-06-2011, 04:01 AM
Datamapper ORM Multi-tabel Relationship Halp! - by El Forum - 08-06-2011, 01:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB