3

示例:我对数据库中的表有以下两个类。

            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
               }
            }

现在我想在一个变量中查询所有用户及其相关帖子。类似的东西:

            $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();

须藤代码在这里:

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