我無法使用php idiorm/paris獲取has_many查詢的結果。以paris site的示例爲例,posts的has_many結果作爲對象返回。idiorm/paris has_many as_array結果集
這太棒了,我可以遍歷對象並訪問各個方法,但我想要做的就是將結果集作爲關聯數組傳遞給我的模板引擎以供顯示。
例子:
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()將整個結果集作爲關聯數組,由c as_array的方式適用於單個行的某些字段,例如
$post_list = $posts()->as_array(id,title,post,date);
這,或類似$用戶上的東西通話>的帖子() - > find_many() - > as_array()不工作。
使用巴黎訪問此類結果集的正確方法是什麼?
這是你應該在擴展'ORM'的類中實現,而不是直接在'ORM'中進行,否則你將無法輕鬆升級。你有沒有想過要把它作爲Idiorm的拉取請求? – Treffynnon 2013-01-15 15:44:10
此功能已添加到Idiom/Paris。 [文檔](http://idiorm.readthedocs.org/en/latest/querying.html?highlight=find_array#as-an-associative-array) – alttag 2014-08-18 14:06:26