Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###### |
| 2 | Models |
| 3 | ###### |
| 4 | |
| 5 | Models are **optionally** available for those who want to use a more |
| 6 | traditional MVC approach. |
| 7 | |
| 8 | - `What is a Model? <#what>`_ |
| 9 | - `Anatomy of a Model <#anatomy>`_ |
| 10 | - `Loading a Model <#loading>`_ |
| 11 | - `Auto-Loading a Model <#auto_load_model>`_ |
| 12 | - `Connecting to your Database <#conn>`_ |
| 13 | |
| 14 | What is a Model? |
| 15 | ================ |
| 16 | |
| 17 | Models are PHP classes that are designed to work with information in |
| 18 | your database. For example, let's say you use CodeIgniter to manage a |
| 19 | blog. You might have a model class that contains functions to insert, |
| 20 | update, and retrieve your blog data. Here is an example of what such a |
| 21 | model class might look like:: |
| 22 | |
| 23 | class Blogmodel extends CI_Model { var $title = ''; var $content = ''; var $date = ''; function __construct() { // Call the Model constructor parent::__construct(); } function get_last_ten_entries() { $query = $this->db->get('entries', 10); return $query->result(); } function insert_entry() { $this->title = $_POST['title']; // please read the below note $this->content = $_POST['content']; $this->date = time(); $this->db->insert('entries', $this); } function update_entry() { $this->title = $_POST['title']; $this->content = $_POST['content']; $this->date = time(); $this->db->update('entries', $this, array('id' => $_POST['id'])); } } |
| 24 | |
| 25 | Note: The functions in the above example use the :doc:`Active |
| 26 | Record <../database/active_record>` database functions. |
| 27 | |
| 28 | .. note:: For the sake of simplicity in this example we're using $_POST |
| 29 | directly. This is generally bad practice, and a more common approach |
| 30 | would be to use the :doc:`Input Class <../libraries/input>` |
| 31 | $this->input->post('title') |
| 32 | |
| 33 | Anatomy of a Model |
| 34 | ================== |
| 35 | |
| 36 | Model classes are stored in your application/models/ folder. They can be |
| 37 | nested within sub-folders if you want this type of organization. |
| 38 | |
| 39 | The basic prototype for a model class is this:: |
| 40 | |
| 41 | class Model_name extends CI_Model { function __construct() { parent::__construct(); } } |
| 42 | |
| 43 | Where Model_name is the name of your class. Class names **must** have |
| 44 | the first letter capitalized with the rest of the name lowercase. Make |
| 45 | sure your class extends the base Model class. |
| 46 | |
| 47 | The file name will be a lower case version of your class name. For |
| 48 | example, if your class is this:: |
| 49 | |
| 50 | class User_model extends CI_Model { function __construct() { parent::__construct(); } } |
| 51 | |
| 52 | Your file will be this:: |
| 53 | |
| 54 | application/models/user_model.php |
| 55 | |
| 56 | Loading a Model |
| 57 | =============== |
| 58 | |
| 59 | Your models will typically be loaded and called from within your |
| 60 | :doc:`controller <controllers>` functions. To load a model you will use |
| 61 | the following function:: |
| 62 | |
| 63 | $this->load->model('Model_name'); |
| 64 | |
| 65 | If your model is located in a sub-folder, include the relative path from |
| 66 | your models folder. For example, if you have a model located at |
| 67 | application/models/blog/queries.php you'll load it using:: |
| 68 | |
| 69 | $this->load->model('blog/queries'); |
| 70 | |
| 71 | Once loaded, you will access your model functions using an object with |
| 72 | the same name as your class:: |
| 73 | |
| 74 | $this->load->model('Model_name'); $this->Model_name->function(); |
| 75 | |
| 76 | If you would like your model assigned to a different object name you can |
| 77 | specify it via the second parameter of the loading function:: |
| 78 | |
| 79 | $this->load->model('Model_name', 'fubar'); $this->fubar->function(); |
| 80 | |
| 81 | Here is an example of a controller, that loads a model, then serves a |
| 82 | view:: |
| 83 | |
| 84 | class Blog_controller extends CI_Controller { function blog() { $this->load->model('Blog'); $data['query'] = $this->Blog->get_last_ten_entries(); $this->load->view('blog', $data); } } |
| 85 | |
| 86 | Auto-loading Models |
| 87 | =================== |
| 88 | |
| 89 | If you find that you need a particular model globally throughout your |
| 90 | application, you can tell CodeIgniter to auto-load it during system |
| 91 | initialization. This is done by opening the |
| 92 | application/config/autoload.php file and adding the model to the |
| 93 | autoload array. |
| 94 | |
| 95 | Connecting to your Database |
| 96 | =========================== |
| 97 | |
| 98 | When a model is loaded it does **NOT** connect automatically to your |
| 99 | database. The following options for connecting are available to you: |
| 100 | |
| 101 | - You can connect using the standard database methods :doc:`described |
| 102 | here <../database/connecting>`, either from within your |
| 103 | Controller class or your Model class. |
| 104 | - You can tell the model loading function to auto-connect by passing |
| 105 | TRUE (boolean) via the third parameter, and connectivity settings, as |
| 106 | defined in your database config file will be used: |
| 107 | :: |
| 108 | |
| 109 | $this->load->model('Model_name', '', TRUE); |
| 110 | |
| 111 | - You can manually pass database connectivity settings via the third |
| 112 | parameter: |
| 113 | :: |
| 114 | |
| 115 | $config['hostname'] = "localhost"; $config['username'] = "myusername"; $config['password'] = "mypassword"; $config['database'] = "mydatabase"; $config['dbdriver'] = "mysql"; $config['dbprefix'] = ""; $config['pconnect'] = FALSE; $config['db_debug'] = TRUE; $this->load->model('Model_name', '', $config); |
| 116 | |
| 117 | |