2017-03-21 69 views
0

我使用cakephp 2.x,我想從我的模型中獲取某個元素作爲字段。問題在於我的結構。如果我用找到所有然後我收到這樣的:蛋糕PHP如何添加數組來查找字段

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) "[email protected]" 
     ["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) "[email protected]KFH.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 protected]" 
     ["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) "[email protected]" 
     ["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) "[email protected]" 
     ["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 protected]" 
     ["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。

我該如何得到類似的東西?我想在後來的編纂中設定價值,但它阻礙了我的工作。

+0

那麼你的問題實際上是什麼?我認爲你需要讓它更清楚。你的查找功能如何?你實際需要什麼結果?在cakephp中使用debug而不是print_r。 –

回答

1

在CakePHP 2,你可以指定要使用fields選項像find()查詢返回的字段: -

$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查詢,所以包含需要爲查詢運行相關模型的參數。