在Rails中,比如说一个博客应用程序,给定一个特定的帖子对象,你可以得到帖子作者的名字,比如:
post = Post.find(1)
author_name = post.author.name
有没有使用DataObject的PHP等价物,类似于(这里只是虚构的语法):
$postsTable = DB_DataObject::factory('posts');
$authorName = (($postsTable->id = 1)->find(true))->author->name;
| finds and autofetches post #1 |->author->name
如果你想要一个像这样带有API的ORM,我建议你使用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
您可能还对Propel ORM:感兴趣
$book = BookQuery::create()
->useAuthorQuery()
->filterByFirstName('Leo')
->endUse()
->with('Author')
->findOne();
$author = $book->getAuthor();
http://www.propelorm.org/wiki/Documentation/1.6/Relationships
DB_DataObject没有流畅的接口,因此无法进行链接。但是你可以这样做:
$postsTable = DB_DataObject::factory('posts');
if($postsTable->get($id)) {
$authorname = $postsTable->getLink('author_id')->name;
}