Re: [CakePHP] 請問 recursive

看板PHP作者 (寶貝豬)時間15年前 (2009/05/19 00:54), 編輯推噓1(104)
留言5則, 1人參與, 最新討論串2/3 (看更多)
官網上面還有這麼一段有關recursive的描述: http://book.cakephp.org/view/439/recursive 簡單講, 它是用來控制find()要找的資料模型(model)對外關聯的層數. 從它舉的 例子來看: -1 代表僅限於model本身, 不對外做join的動作. 0 代表本身, 以及其所belongs to 的model. 1 代表本身, 以及其所belongs to 的model, 還有其所have many(has one)的model 2 代表1 所包含的範圍, 並更深入下一層的model. Imagine your application features Groups which belong to a domain and have many Users which in turn have many Articles. 就以 groups, domain, users, articles 的關聯來舉例, 大概就抓得到recursive 在這裏的用途. 以下補充個人經驗: 我們在用cake 做快速塑模時, 就已先把model之間的關聯都建立好了, 當然也包括 primary key與foreign key的聯結. 而後續在程式當中要用find()函式來撈資料 時, 也必須手動地在$fields當中加入foreign key. 當然底下那段話的意思也能抓了個8成準了. ※ 引述《HuangJC (吹笛牧童)》之銘言: : 同事的程式中有這一句 : $this->User->findByEmail($email, null, null, -1) : 經翻查 findBy 的章節,help 是這樣寫的 : findBy<fieldName>(string $value) : 難怪我上次學習 Cake 時,大家看不懂這是什麼 : 但如果去 trace source code,會發現它真的能動! : (X啦,超過文件的規格可以視為 side effect 呀!!) : 好吧,我就繼續 trace,發現 recursive 其實在別的章節找得到文件 : 比如在 findAll : findAll(string $conditions, array $fields, string $order, int $limit, int : $page, int $recursive) : findAll has been deprecated, use find('all') instead. : 這句有趣吧,help 說 findAll 已經不建議使用了 : 但是 CakePHP 內部還是用它 implement 的,用得很爽 : 換言之就是 'UnDocument' (應該說文件不推薦才對,因為文件還是把它列出了) : 那什麼是 recursive 呢? : Setting the $recursive parameter to an integer forces findAll() to fetch data : according to the behavior described in the Model Attributes $recursive : section outlined earlier. Do not forget to manually add the required foreign : key columns to the $fields array as described there. : 以上,整段翻譯對我有些困難 ~^_^~ : 麻煩講解了 : 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.104.190.247 ※ 編輯: bobju 來自: 59.104.190.247 (05/19 01:00)

05/19 01:15, , 1F
recursive 就字面來說是遞迴,本來一直想不到什麼東西
05/19 01:15, 1F

05/19 01:15, , 2F
在遞迴;不過提到 join,且 model 間的關係可以先設定
05/19 01:15, 2F

05/19 01:16, , 3F
cake source code 中可看出每一層 -1 的動作;但另一
05/19 01:16, 3F

05/19 01:17, , 4F
個可能的問題是洪水太泛濫拖慢速度,如果不補 foreign
05/19 01:17, 4F

05/19 01:17, , 5F
key 的下層 model 就不會 join,那就更好了..
05/19 01:17, 5F
文章代碼(AID): #1A4PAuDw (PHP)
文章代碼(AID): #1A4PAuDw (PHP)