dreamact3026 2017-03-21 11:46
浏览 27
已采纳

Cake PHP如何添加数组来查找字段

I'm using cakephp 2.x and I would like to get certain element as field from my model. the problem is with my structure. If I use find all then I recieve this:

array(9) {
  [0]=> ...
  [1]=>
  array(7) {
    ["Person"]=>
    array(18) {
      ["id"]=>
      int(2)
      ["imie"]=>
      string(6) "STEFAN"
      ["nazwisko"]=>
      string(8) "ŚMIAŁY"
      ["tel"]=>
      string(13) "+55 648989748"
      ["mob"]=>
      string(13) "+87 489747489"
      ["email"]=>
      string(25) "HSDJKAHSDJK@HSDFJASHF.CIJ"
      ["tytul_naukowy"]=>
      string(7) "TEZ NIE"
      ["data_od"]=>
      string(10) "2017-02-16"
      ["data_do"]=>
      NULL
      ["object_id"]=>
      int(-1)
      ["object_type_id"]=>
      int(2)
      ["duty_id"]=>
      int(28)
      ["created"]=>
      string(22) "2017-02-16 13:22:32+01"
      ["modified"]=>
      string(22) "2017-02-16 13:22:32+01"
      ["deleted"]=>
      int(0)
      ["additional_email"]=>
      string(17) "UDIHAS@HFSDKFH.OK"
      ["description"]=>
      string(4) "OPIS"
      ["displayName"]=>
      string(15) "STEFAN ŚMIAŁY"
    }
    ["Duty"]=>
    array(3) {
      ["id"]=>
      int(28)
      ["nazwa"]=>
      string(20) "KOORDYNATOR RECEPCJI"
      ["atrybut"]=>
      string(1) "W"
    }
    ["ObjectType"]=>
    array(3) {
      ["id"]=>
      int(2)
      ["nazwa"]=>
      string(4) "Obks"
      ["model"]=>
      string(3) "Obk"
    }
    ["Cro"]=>
    array(12) {
      ["id"]=>
      NULL
      ["created"]=>
      NULL
      ["nazwa"]=>
      NULL
      ["tel"]=>
      NULL
      ["fax"]=>
      NULL
      ["email"]=>
      NULL
      ["www"]=>
      NULL
      ["modified"]=>
      NULL
      ["deleted"]=>
      NULL
      ["cro_type_id"]=>
      NULL
      ["displayName"]=>
      NULL
      ["objectTypeIdWithId"]=>
      NULL
    }
    ["Attachment"]=>
    array(0) {
    }
    ["Specialization"]=>
    array(0) {
    }
    ["Obk"]=>
    array(1) {
      [0]=>
      array(13) {
        ["id"]=>
        int(2)
        ["nazwa"]=>
        string(10) "OBK Z ID 2"
        ["tel"]=>
        string(13) "+48 123456890"
        ["fax"]=>
        string(13) "+48 123456789"
        ["email"]=>
        string(15) "EMAIL@EMAIL.COM"
        ["www"]=>
        string(11) "WWW.WWW.WWW"
        ["modified"]=>
        string(22) "2017-03-20 15:11:31+01"
        ["created"]=>
        string(22) "2014-05-12 21:50:17+02"
        ["deleted"]=>
        int(0)
        ["local_order"]=>
        int(3)
        ["displayName"]=>
        string(10) "OBK Z ID 2"
        ["objectTypeIdWithId"]=>
        string(3) "2_2"
        ["PeopleObk"]=>
        array(3) {
          ["id"]=>
          int(1)
          ["person_id"]=>
          int(2)
          ["obk_id"]=>
          int(2)
        }
      }
    }
  }
  [2]=>
  array(7) {
    ["Person"]=>
    array(18) {
      ["id"]=>
      int(3)
      ["imie"]=>
      string(5) "KAMIL"
      ["nazwisko"]=>
      string(10) "STEFAŃSKI"
      ["tel"]=>
      string(13) "+23 123123123"
      ["mob"]=>
      string(13) "+21 412312312"
      ["email"]=>
      string(16) "ASDASD@SDFDSF.LS"
      ["tytul_naukowy"]=>
      string(3) "NIE"
      ["data_od"]=>
      string(10) "2017-02-22"
      ["data_do"]=>
      NULL
      ["object_id"]=>
      int(-1)
      ["object_type_id"]=>
      int(2)
      ["duty_id"]=>
      int(26)
      ["created"]=>
      string(22) "2017-02-22 16:14:32+01"
      ["modified"]=>
      string(22) "2017-02-22 16:14:32+01"
      ["deleted"]=>
      int(0)
      ["additional_email"]=>
      string(22) "ADAM.2313@WEBIMPULS.PL"
      ["description"]=>
      string(4) "TEST"
      ["displayName"]=>
      string(16) "KAMIL STEFAŃSKI"
    }
    ["Duty"]=>
    array(3) {
      ["id"]=>
      int(26)
      ["nazwa"]=>
      string(41) "KOORDYNATOR OŚRODKÓW BADAŃ KLINICZNYCH"
      ["atrybut"]=>
      string(1) "W"
    }
    ["ObjectType"]=>
    array(3) {
      ["id"]=>
      int(2)
      ["nazwa"]=>
      string(4) "Obks"
      ["model"]=>
      string(3) "Obk"
    }
    ["Cro"]=>
    array(12) {
      ["id"]=>
      NULL
      ["created"]=>
      NULL
      ["nazwa"]=>
      NULL
      ["tel"]=>
      NULL
      ["fax"]=>
      NULL
      ["email"]=>
      NULL
      ["www"]=>
      NULL
      ["modified"]=>
      NULL
      ["deleted"]=>
      NULL
      ["cro_type_id"]=>
      NULL
      ["displayName"]=>
      NULL
      ["objectTypeIdWithId"]=>
      NULL
    }
    ["Attachment"]=>
    array(0) {
    }
    ["Specialization"]=>
    array(0) {
    }
    ["Obk"]=>
    array(1) {
      [0]=>
      array(13) {
        ["id"]=>
        int(3)
        ["nazwa"]=>
        string(10) "OBK Z ID 3"
        ["tel"]=>
        string(13) "+48 123456890"
        ["fax"]=>
        string(13) "+48 123456789"
        ["email"]=>
        string(15) "EMAIL@EMAIL.COM"
        ["www"]=>
        string(11) "WWW.WWW.WWW"
        ["modified"]=>
        string(22) "2017-03-20 10:45:24+01"
        ["created"]=>
        string(22) "2014-05-16 09:09:33+02"
        ["deleted"]=>
        int(0)
        ["local_order"]=>
        int(1)
        ["displayName"]=>
        string(10) "OBK Z ID 3"
        ["objectTypeIdWithId"]=>
        string(3) "2_3"
        ["PeopleObk"]=>
        array(3) {
          ["id"]=>
          int(2)
          ["person_id"]=>
          int(3)
          ["obk_id"]=>
          int(3)
        }
      }
    }
  }

And I want just to retrieve Person.id, Person.displayName and Obk.0.id. The two first are no problem, but wheneve I try to get Obk.0.id I'm getting error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM for Table Obk.

How can I get something like that? I want to set the value in codition later, but it block my work.

  • 写回答

1条回答 默认 最新

  • doushi7314 2017-03-21 16:04
    关注

    In CakePHP 2 you can specify the fields you want returned by a find() query using the fields option like:-

    $this->Foo->find('all', [
        'fields' => ['Foo.id', 'Foo.name']
    ]);
    

    This also works with contained models that have a belongsTo or hasOne relationship:-

    $this->Foo->find('all', [
        'fields' => ['Foo.id', 'Foo.name', 'Bar.name'],
        'contain' => ['Bar']
    ]);
    

    However, if the contained model is a hasMany or HABTM relationship you need to specify the fields of that model on the contain:-

    $this->Foo->find('all', [
        'fields' => ['Foo.id', 'Foo.name'],
        'contain' => [
            'Bar' => ['fields' => 'Bar.name']
        ]
    ]);
    

    This is because CakePHP will be performing multiple SQL queries, so the contain needs to have the parameters for the query being run for the relevant model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?