【发布时间】:2012-02-10 14:37:05
【问题描述】:
我无法使用 php idiorm/paris 获取 has_many 查询的结果。按照paris site 中的示例,帖子的has_many 结果作为对象返回。
太好了,我可以遍历对象并访问各个方法,但我想做的是将结果集作为关联数组传递给我的模板引擎进行显示。
例子:
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
}
}
API 是这样工作的:
// 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();
我可以像这样访问帖子对象并打印结果集:
// echo each post id
foreach ($posts as $post) {
echo $post->id;
}
不过,我真正想做的是使用 as_array() 将整个结果集作为关联数组获取,受 as_array 用于单个行的方式中的某些字段限制,例如
$post_list = $posts()->as_array(id,title,post,date);
这样,或者像 $user->posts()->find_many()->as_array() 这样的调用都不起作用。
使用 paris 访问此类结果集的正确方法是什么?
【问题讨论】: