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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能