Re: New PDO-based DBAL/ORM for PEAR2
| From: | Bertrand Mansion | Date: | Wed, 21 Nov 2007 17:29:12 +0000 |
| Subject: | Re: New PDO-based DBAL/ORM for PEAR2 | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-48555@lists.php.net to get a copy of this message | ||
Le 21 nov. 07 à 17:24, Michael J. I. Jackson a écrit :
So I can either manage a list of connection aliases in my head, or I can let PDORM do it for me and just use PDORM::singleton(). I personally think the latter is easier and requires less work on my part.That's personal I guess, since I find it easier to remember an alias like 'db1' or 'db2' than my password :)
I named my object Session, but it has nothing to do with PHP sessions. I think I could have named them Connection instead, but they store more info than just connections. Session is a term from Hibernate which inspired some of my developments.Thinking about it, my Session objects might look like your Domains and hold information about objects already loaded(?).Does the fact that you're using sessions mean that you are actually using PHP sessions? How do you restore the connection upon subsequent requests? Do you keep it open between requests? IMO, that's interfering too much with the session...the user might not even want to use sessions in his application...please clarify if I'm way off here.
I can initialize my relations using three different ways depending on my coding style and my requirements. A relation is represented by a PHP array like this : 1. One-to-one and one-to-many ----------------------------- NB: I don't make a difference between 1:1 and 1:n because I have methods like fetchOne()/fetchMany() and set...()/add...() that do the difference. array('book', array('author_id' => 'id'), 'author') This defines a join between book and author using book.author_id and author.id I don't like the Ruby on Rails mojo "Convention over Configuration", too much magic involved, I prefer my joins to be explicit. When added to the registry, this definition creates a relation object and eventually the reverse relation. 2. Many-to-many --------------- I use pivot tables for that. array('book', array('id' => 'book_id'), array('book_author' => 'author')) array('author', array('id' => 'author_id'), array('book_author' => 'book')) If I also want to use the reverse relation I have to declare it as well, thus the second array. These relation definitions can then be passed directly to the relation registry: $registry = Phreez::getRelationRegistry(); $registry->addRelation(array(...)); If I use subclasses, I don't need to do that. All I have to do is have a public static method called initRelations() return the array containing the relations or have the relation stored in a public static property, which one I use depends on the logic behind my relations, the static method offers more flexibility. class Classes_Book extends Phreez_Object { public static function initRelations() { $rel[] = array('book', array('publisher_id' => 'id'), 'publisher'); $rel[] = array('book', array('category_id' => 'id'), 'category'); $rel[] = array('book', array('id' => 'book_id'), array('book_author' => 'author')); return $rel; } } Since I use a custom autoload (not the one in PHP5), when my objects are first loaded (they are loaded on request, for example when $author->getBooks() is called and I can force a class to be used instead of another), their initRelations() method is called, statically. -- Bertrand Mansion Mamasam Work : http://www.mamasam.com Blog : http://golgote.freeflux.netFor handling relations, I don't use RoR has_many etc, I think it's not good. I use a RelationRegistry that hods information about all the relations between objects in the database. Relations are represented as objects, they are able to automatically create joined queries for select, updates, etc. The RelationRegistry can be queried at any time by any other object.Is your registry an XML file? How do you initially set up the relations? I am all for finding a better way to do this because I'm not sure that ROR has the best implementation, but it's a good one.