【发布时间】:2012-10-05 03:06:44
【问题描述】:
当我想使用 ORM 查询表时遇到问题,例如我有带有字段 id、作者、文本的文章表。
我的代码是这样的:
// Single where
$article = Model_Article::find()->where('id', 4);
print_r($article);
该代码不会获取表格文章上的所有字段,就像select * from article where id = 4
尝试可能性
$article = Model_Article::find(null, array('id','title'))->where('id', 3);
反应是
object(Orm\Query)#89 (14) {
["model":protected]=>
string(10) "Model_Article"
["connection":protected]=>
NULL
["view":protected]=>
NULL
["alias":protected]=>
string(2) "t0"
["relations":protected]=>
array(0) {
}
["joins":protected]=>
array(0) {
}
["select":protected]=>
array(1) {
["t0_c0"]=>
string(5) "t0.id"
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["rows_limit":protected]=>
NULL
["rows_offset":protected]=>
NULL
["where":protected]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(9) "and_where"
[1]=>
array(3) {
[0]=>
string(5) "t0.id"
[1]=>
string(1) "="
[2]=>
int(3)
}
}
}
["order_by":protected]=>
array(0) {
}
["values":protected]=>
array(0) {
}
}
那不是返回 id 或标题字段。
但是当我尝试添加 get_one() 方法时
$article = Model_Article::find(null, array('id','title'))->where('id', 3)->get_one();
id 是 return ,但 title 不是,另外一个字段,我不知道为什么?
参考
- ORM Discussion FuelPHP 据说 ORM 目前将选择所有列,目前没有计划更改。
我的问题
- 使用像这样
select id,owner from article where id = 4的 ORM 选择自定义字段,它将只返回 id 和所有者,是否可以在 FUELPHP 上使用 ORM 来获得它?
【问题讨论】:
-
我现在不在家,所以试试这个并交叉手指 ;-) : $article = Model_Article::find(null, array('id', 'owner'))->其中('id',4);阅读框架的代码这应该可以工作,让我知道,这样我就可以为此写一个答案。
-
在尝试您的建议后我会更新我的问题...:)