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

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.

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.