SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Table-Data-Gateway and one-to-many relations

    Hello folks,

    The past year I was reading much about MVC, Design-Patterns, TDD, etc. but never really tried it out.
    I have one question about the Table-Data-Gateway:
    Is it possible to manage a one-to-many relationship between two tables, let's say a blogentry and it's comments? In another forum I read that this pattern is only used to manage CRUD-operations on a table and nothing more? So if I've a 1:n relation I've to stick to the ActiveRecord pattern?

    Will this be beyond the scope of the TDG?
    PHP Code:
    $entry BlogFinder::findById(1);
    $comments $entry->hasMany('comments')->get(); 
    Btw: I do not have a copy of PoEAA yet

  2. #2
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see that as being outside the scope of TDG. How would you do it with ActiveRecord?

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That depends. The moment you begin to abstract out relations, you're going down the murky path of ORM. In-memory objects and relational databases are fundamentally different in how they represent relationships. This means that there is no solid way of abstracting it completely, without crippling your relational model.

  4. #4
    SitePoint Zealot
    Join Date
    May 2008
    Location
    Montreal
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken is right, although there are subtleties to the point that many don't realize. ORMs certainly do make relations of certain types easy to work with from a distance; however, the gain in simplicity with relations is offset by the loss in expressive power when one wants to work with other important aspects of the SQL language. ORMs tend to abstract SQL away and in doing so often limit what you can put into a query and how easy it is to put something into a query.

    On a final note, I've experienced through making and using ORMs that the convenience of certain types of relations often obscures what's really going on. Sometimes getting related data is so easy that one doesn't realize that an extra query is being performed, possibly within a large loop, and that the data can be fetched in more efficient ways.

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So doing something like the following would be ok?

    PHP Code:
    <?php
    abstract class TableDataGatewayBase
    {
        protected 
    $_hasOne = array();

        
    /**
         * The array has a structure like:
         *
         * array('authors' => 'author')
         */
        
    protected $_hasMany = array();

        protected 
    $_belongsTo = array();

        
    /** CRUD operations **/
        
    public function create($row);
        public function 
    read($pk);
        public function 
    update($row);
        public function 
    delete($pk);

        public function 
    hasMany($what)
        {
            if(!
    in_array($this->_hasMany$what))
            {
                return 
    false;
            }

            if(!
    $this->_hasMany[$what] instanceof TableDataGatewayBase)
            {
                
    $this->_hasMany[$what] = Finder::findForParent($what$this->getId()); // probably bad naming, sth like findBelongingTo would be better?
            
    }

            return 
    $this->_hasMany[$what];
        }
    }
    ?>
    Please be forgiving, if I totally missed the point, but practically I'm new to this stuff And my english isn't really making the whole thing easier

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    706
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Read through some of the documentation on the PHP Doctrine site http://www.doctrine-project.org/

    Maybe even download it and try out some of the examples. That should give you a better idea of what kyberfabrikken means when he talks about the murky path of ORM.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •