2

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
4

2 に答える 2

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

于 2011-06-03T16:42:35.230 に答える
0

DB_DataObject には流暢なインターフェースがないため、チェーンできません。ただし、これを行うことができます:

$postsTable = DB_DataObject::factory('posts');
if($postsTable->get($id)) {
  $authorname = $postsTable->getLink('author_id')->name;
}
于 2011-07-01T07:53:52.320 に答える