dpa55065 2012-10-05 03:06
浏览 36
已采纳

是否可以在Fuelphp上使用ORM自定义选择字段?

I have a problem when I want to query table using ORM ,example I have article table with field id,author,text.

My code like this :

// Single where
$article = Model_Article::find()->where('id', 4);
print_r($article);

that't code will be fetch all field on table article, it's like select * from article where id = 4

Try Possibility

$article = Model_Article::find(null, array('id','title'))->where('id', 3);

the response is

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

that's is not return id or title field.

but when i'm try by adding get_one() method

$article = Model_Article::find(null, array('id','title'))->where('id', 3)->get_one();

id is return , but title is not and another field, i don't know why ?

Reference

  1. ORM Discussion FuelPHP it's say ORM currently will be select all column, no plans to change that at the moment.

My Problem

  1. Select Custom Field using ORM like this select id,owner from article where id = 4 it's will be return only id & owner, Is Possible to get that using ORM on FUELPHP ?
  • 写回答

4条回答 默认 最新

  • duankun9280 2012-10-31 19:47
    关注

    Do not use

    Model_Article::find()->
    

    but use

    Model_Article::query()->
    

    The first one works but is considered an error situation which might change in future versions.

    As of version 1.4 the ORM supports partial selects, using

    Model::query()->select('id', 'value')->
    

    The second parameter of find() is an array of conditions for the find, such as 'where' or 'order_by' clauses. There is no support for selecting column names in this array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。