Re: [請益] 初學CodeIgniter的一些問題

看板PHP作者 (任性)時間15年前 (2010/04/24 22:44), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
※ 引述《tkdmaf (皮皮快跑)》之銘言: : ※ 引述《roga (任性)》之銘言: : : 推 tkdmaf:$this->db->xxxx不是繼承自model嗎? : : 不是,在 CodeIgniter 裡面 DB 是一個 Library : : 如果你沒有在 autoload 裡面載入,你必須手動 $this->load->database(); : : 這樣你的 $this->db->query(); 才會有用,要不然是 undefined method. : : 另外 : : $this->load->model('Model_name', 'example'); : : $this->example->function(); 才是在操作 Model 。 : : 請先把文件全部看完,就不會有這些疑問了 : : http://codeigniter.com/user_guide/ : 基本上………這應該算「是」吧! 有些事情要先釐清 1. db driver 的目錄(裡面包含抽象層和實做)和 lib 放在同一層 2. 在 autoload 裡面 db 被歸類在 lib 內 3. db driver 是透過 system/libraries/Loader.php 載入 順道一提, model 也在這邊載入。 但其實它是一個系統功能,從原始碼看來, 它的地位是和 Library 一樣高的,如果你注意到語法的話, 會發現呼叫它的載入方式屬於這個等級 $this->load->database(); ... $this->load->library('encrypt'); $this->load->model('Model_name'); $this->load->helper('string'); ..etc see also: http://codeigniter.com/user_guide/images/appflowchart.gif
: 在繼承model之後,$this->db->xxxxx就不需要額外定義可在model中使用。 : 這個疑問最初的來源就是基於我故意在controller什麼引用方法都不使用時。 : 在controller中$this->db->xxxxx是不能用的,但在model中是可以的。 "$this->db" comes from system/libraries/Loader.php line 201 the implementation: function database($params = '', $return = FALSE, $active_record = FALSE) { // Grab the super object $CI =& get_instance(); ..中略 // Initialize the db variable. Needed to prevent // reference errors with some configurations $CI->db = ''; // Load the DB class $CI->db =& DB($params, $active_record); // Assign the DB object to any existing models $this->_ci_assign_to_models(); } } : 原則上因為習慣性都是把和資料相關的功能在model中使用。 : 平常倒沒有習慣會把$this->db->xxxxx的語法使用在controller中以免混淆。 : 原則上我覺得這樣的設計應該就是要讓使用者習慣於把資料存取的功能寫在 : model裡而不要去和controller混淆使用吧。 : codeigniter用到現在才一個多月,有什麼錯誤請指正。 -- The Internet: where men are men, women are men, and children are FBI agents. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.220.104.63 ※ 編輯: roga 來自: 61.220.104.63 (04/24 23:49)
文章代碼(AID): #1BqmFW6n (PHP)
文章代碼(AID): #1BqmFW6n (PHP)