私はParis ( Idiormの上に構築されています) を使用しています。
私は次の Model クラスを持っています (例は github のドキュメントからインスピレーションを得ています):
<?php
class Post extends Model {
public function user() {
return $this->belongs_to('User');
}
}
class User extends Model {
public function posts() {
return $this->has_many('Post'); // Note we use the model name literally - not a pluralised version
}
}
だから今、私は次のことができます(うまくいきます):
// Select a particular user from the database
$user = Model::factory('User')->find_one($user_id);
// Find the posts associated with the user
$posts = $user->posts()->find_many();
// Select a particular post from the database
$post = Model::factory('Post')->find_one($post_id);
// Find the user associated with the post
$user = $post->user()->find_one();
しかし、私は次のこともやりたいです:
$posts = Model::factory('Post')->find_many();
foreach ($posts as $post) {
echo($post->user()->find_one()->username); // generates a query each iteration
}
残念ながら、これは反復ごとにクエリを作成します。最初の find_many クエリで関連情報を取得するように Paris または Idiorm に指示する方法はありますか?
クエリの数を最小限に抑えるために、パリで情報を取得するにはどうすればよいですか? 結合条件を手動で指定する必要がないようにしたい (これが、Idiorm ではなく Paris を使用している理由です)