SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Active Record implementation for PHP

    Hi,

    I'm looking to learn more about the Active Record implementation in PHP. Any reason not to implement this (i know its not as pretty as it would be in ruby but its still a nice implementation)?

    Books, sites, tutorials?

    Thanks if advance for your recommendations.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont like Active Record at all, check out this link for further explanation

  3. #3
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Porte

    Hi, you might want to check Porte by Adaltas which is a decent implementation and with a lot of tests to learn from. It is released under a BSD license

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a fantastic active record implementation on PHP, I don't think you can go past Doctrine: http://www.phpdoctrine.org/

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also get interested in Limb3 ActiveRecord implementation(seems like it was inspired by Rails). Unfortunately, there's no English documentation atm(only Russian), but there are lot of tests which pretty much show what it's capable of - https://svn.limb-project.com/limb/3....d/tests/cases/

  6. #6
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As always, I recommend ADOdb, which has a pretty good implementation of AR.

  7. #7
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by myc View Post
    I dont like Active Record at all, check out this link for further explanation
    Actually, this article doesn't refute ActiveRecord at all -- it just explains why ActiveRecord is not the same as the Model in MVC pattern, and why do Rails, Cake and other patterns get it wrong.

    In my internal framework I use ActiveRecord as well, but exactly for what it is -- a data layer abstraction. There is a separate model implementation (actually several of them, differing between various types of models, so I have a separate Collection abstract class), which is using AR as the way to communicate with the SQL server. When I needed to replace SQL with Amazon's SimpleDB, all I had to do is replace the AR with some new code, leaving the actual model's interface and usage intact.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by myc View Post
    I dont like Active Record at all, check out this link for further explanation
    But he is wrong. AR is very well suited to act as the M in MVC. Consider Fowlers definition of AR (emphasis mine):

    An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.
    ...
    Active Record uses the most obvious approach, putting data access logic in the domain object.
    You can perfectly well use AR objects as domain objects, because they are just domain objects with encapsulated data access.

    This guy homes in on the fact that AR is used for automatic persistence, hence he places it in the data access layer which he believes then automatically excludes it from domain logic. But it doesn't. AR is both, acting as a bridge between domain logic and data access. When using it from the domain logic side you don't see the data access. When using it from the data access side you don't see the domain logic side.

    I think he is confused by the "gravity" of AR in Rails. The Rails AR implementation is really slick (also encapsulating relationships/associations) so you tend to think of Rails AR as the only way to make model objects.

    But that is wrong. Rails webMVC does not demand that the M=AR. AR is just so often used as the M that a lot of developers tend to forget other types of M objects. You can use any other object as M objects. Could you use a data mapper instead? Sure, but you would then have to design the domain classes yourself (no help from the database schema).

    Now, is AR the perfect pattern for the persistence layer? No it is not. The potential problem with AR is that it only works well when the database design is tightly coupled with the domain representation, i.e. when you as a developer has full control over the database design and can (and do) change it whenever the domain logic demands it. For at lot of projects this is not a concern at all, because the database is really only used for that particular website.

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any way we can make Honeymonster's last paragraph pop up any time anyone mentions ActiveRecord !!!??
    Zealotry is contingent upon 100 posts and addiction 200?

  10. #10
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shea View Post
    For a fantastic active record implementation on PHP, I don't think you can go past Doctrine: http://www.phpdoctrine.org/
    Thanks to this post I found one of the best PHP Classes ever. This is the *BEST* ORM implementation. Efficient, fast and optimized.

    I would go for it.

  11. #11
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about
    PHP Code:
    <?php
    $sql 
    query(array(
        
    'select' => '*',
        
    'from' => 'table',
        
    'where' => array('id''='1'AND''name''=''DomKiller')
        
    'limit' => array('10,5')
    )
    );
    ?>

  12. #12
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've already used that way. But would change this bit:

    PHP Code:
        'where' => array('id' => 1'name' => 'DomKiller'
    PHP Code:
        'where' => array('or' => array('id' => 1'name' => 'DomKiller')) 



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
  •