2

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
jefflunt
  • 33,527
  • 7
  • 88
  • 126

2 Answers2

1

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

Christopher Manning
  • 4,527
  • 2
  • 27
  • 36
0

DB_DataObject does not have a fluent interface so you cannot chain. However you can do this :

$postsTable = DB_DataObject::factory('posts');
if($postsTable->get($id)) {
  $authorname = $postsTable->getLink('author_id')->name;
}
demental
  • 1,444
  • 13
  • 25