3

例: データベースのテーブルに次の 2 つのクラスがあります。

            class Post extends Model {}
            class User extends Model {
               public function posts() {
                   return $this->has_many('Post'); // Note we use the model name literally - not a    pluralised version
               }
            }

ここで、すべてのユーザーとそれに関連する投稿を 1 つの変数でクエリしたいと考えています。そんな感じ:

            $user = Model::factory('User')->find_many();
            // Find the posts associated with the user
            $posts = $user->posts()->find_many();

$posts変数で結果を取得したい。

4

1 に答える 1

3

私はこの問題を自分で解決しました

            $user = Model::factory('User')->find_many();
            // Find the posts associated with the user
            //$posts = $user->posts()->find_many();

$user をループして、各ユーザー ID を $user->posts()->find_one(); に渡しました。

sudo コードは次のとおりです。

            foreach($user as $usr)
            {
               $posts = $user->posts()->find_one($usr->Id);
            }
于 2015-03-10T07:15:50.130 に答える