Re: [CakePHP] 請問 recursive
官網上面還有這麼一段有關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
05/19 01:15, 1F
→
05/19 01:15, , 2F
05/19 01:15, 2F
→
05/19 01:16, , 3F
05/19 01:16, 3F
→
05/19 01:17, , 4F
05/19 01:17, 4F
→
05/19 01:17, , 5F
05/19 01:17, 5F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):