例子: 我有以下两个类用于数据库中的表。
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 变量中获取结果。
最佳答案
我自己解决了这个问题
$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);
}
关于php - 在巴黎需要帮助 php 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916396/