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

时间:2017-03-21 11:46:16

标签: php arrays database cakephp

我正在使用cakephp 2.x,我希望从我的模型中获取某个元素作为字段。问题出在我的结构上。如果我使用find all,那么我接受这个:

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

我想要检索Person.id,Person.displayName和Obk.0.id.首先是两个没有问题,但是当我尝试获取Obk.0.id时我得到错误:SQLSTATE [42P01]:未定义的表:7错误:表Obk缺少FROM。

我怎么能得到这样的东西?我想稍后在编码中设置值,但它会阻止我的工作。

1 个答案:

答案 0 :(得分:1)

在CakePHP 2中,您可以使用find()选项指定fields查询返回的字段,如: -

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

这也适用于具有belongsTohasOne关系的所包含模型: -

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

但是,如果包含的模型是hasMany或HABTM关系,则需要在包含中指定该模型的字段: -

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

这是因为CakePHP将执行多个SQL查询,因此包含需要为相关模型运行查询的参数。