@Paul Apostol: Wow, 60 tables! I can see why you prefer to group them into sub-directories, lol. It wouldn’t be too difficult for me to modify the __autoload function to search sub-directories within the models folder, if the class isn’t found in the same folder as DataMapper. I’ll add that to the Road Map.
I’m looking at including a table prefix, which will apply to both the normal and joining tables. Do you have different prefix’s for your tables? Using the prefix as a way to group them? I guess this could be accommodated when I introduce prefixes, but in the case of having different prefixes for different models, you would end up having to specify that setting in each of the models, rather than just the main DataMapper model.
The id does have to be “id” and to clarify for the joining tables, it’s the singular of the table followed by “_id”. I don’t plan on changing this naming convention.
When setting up a relationship, you need to specify the $has_many or $has_one in both models. Lets say we have Author, Book and Genre objects, with a Many to Many relationship between the Author and Book and a One to Many between the Book and Genre. You would have the following tables:
Normal
authors
books
genres
Joining
authors_books
books_genres
And the models setup like so:
Author model
<?php
class Author extends DataMapper
{
$has_many = array("book" => "books");
class Author()
{
parent::DataMapper();
}
}
Book model
<?php
class Book extends DataMapper
{
$has_many = array("author" => "authors");
$has_one = array("genre" => "genres");
class Book()
{
parent::DataMapper();
}
}
Genre model
<?php
class Genre extends DataMapper
{
$has_many = array("book" => "books");
class Genre()
{
parent::DataMapper();
}
}
With that done, you can access the authors from a book, and the books from an author, and onto the genres from there etc.
$a = new Author();
$a->get();
echo $a->name . ' has written these books:
';
$a->book->get();
foreach ($a->book->all as $b)
{
$b->genre->get();
echo $b->title . ' (' . $b->genre->name . ')
';
}
Going the reverse way:
$g = new Genre();
$g->get();
echo 'The following books are in the ' . $g->name . ' genre:
';
$g->book->get();
foreach ($g->book->all as $b)
{
echo $b->title . ' was written by:
';
$b->author->get();
foreach ($b->author->all as $a)
{
echo $a->name . '
';
}
}
@Boyz26: I’ll setup the same stuff you’ve mentioned and see what happens for me. Stay tuned.