Search code examples
phppeardata-objects

PHP PEAR/DataObject equivalent to Rails' easy ActiveRecord finding functionality


In Rails, say a blogging application, given a particular post object you could get the name of the author of the post doing something like:

post = Post.find(1)    
author_name = post.author.name

Is there a PHP equivalent using DataObject, something like (just making up imaginary syntax here):

$postsTable = DB_DataObject::factory('posts');
$authorName = (($postsTable->id = 1)->find(true))->author->name;

              |  finds and autofetches post #1  |->author->name

Solution

  • If you want an ORM with an API like that I would recommend PHP ActiveRecord:

    $posts = Post::find('all', array('limit' => 10, 'include' => array('author')));
    foreach ($posts as $post) {
       echo $post->author->first_name;
    }
    

    http://www.phpactiverecord.org/projects/main/wiki/Finders


    You may also be interested in Propel ORM:

    $book = BookQuery::create()
      ->useAuthorQuery()
        ->filterByFirstName('Leo')
      ->endUse()
      ->with('Author')
      ->findOne();
    $author = $book->getAuthor();
    

    http://www.propelorm.org/wiki/Documentation/1.6/Relationships