Documentation:Models
From TinyMVC
Contents |
What is a model?
Models are the layer of the MVC that are used to aggregate data. The underlying data source is typically a database, although this can be any type of source such as and LDAP directory or flat file.
Learning by example
Model files live in the models directory. Lets create a model:
/myapp/
/models/
page_model.php
page_model.php
class Page_Model extends TinyMVC_Model { function get_title() { return 'Hello'; } function get_body_text() { return 'Hello World.'; } }
The model file name must match the model class name. The class can be any case, but the file name must be lower case.
The _model extension is not required, but it helps keep things organized.
We load models from within the controller. Lets try an example:
/myapp/controller/hello.php
class Hello_Controller extends TinyMVC_Controller { function index() { // load the model $this->load->model('Page_Model','page'); // use the model to gather data $title = $this->page->get_title(); $body_text = $this->page->get_body_text(); $this->view->assign('title',$title); $this->view->assign('body_text',$body_text); $this->view->display('hello_view'); } }
The first parameter to $this->load->model() is the model class name. The second parameter is an optional alias name. This loads the model into a class property, then you use it to gather data. If you kept the same view file from the previous chapter, you should see the same result except this time the assigned data came from a model.
Using the PDO database layer
TinyMVC uses the PDO database abtraction layer for database access, which is included with PH5 5.1 and up. Be sure you have your myapp/configs/database.php setup for your database.
Lets try an example:
members_model.php
class Members_Model extends TinyMVC_Model { function get_members() { $this->db->query('select * from members'); while($row = $this->db->next()) $results[] = $row; return $results; } }
Assuming you have a database with a table named members, the above get_members() method would return all the records in the members table.
class Members_Model extends TinyMVC_Model { function get_member($id) { return $this->db->query_one('select * from members where id=?',array($id)); } }
The above gets one record from the database. This example shows how to use a variable passed into the method as a query parameter. query_one() returns one record.
class Members_Model extends TinyMVC_Model { function get_members() { return $this->db->query_all('select * from members'); } }
query_all() returns all records from a query. This is essentially the same as the first query() example.
You can optionally pass a format value of PDO::FETCH_ASSOC, PDO::FETCH_NUM, or PDO::FETCH_BOTH. PDO::FETCH_ASSOC is default.
Available functions:
- $this->db->query($sql,$params) executes a query to loop over results with $this->db->next()
- $this->db->query_one($sql,$params,$format) returns one result record
- $this->db->query_all($sql,$params,$format) returns all result records
- $this->db->next($format) used to loop over a result set
- $this->db->last_insert_id() gets the last insert id from the database
- $this->db->num_rows() returns the number of records from the last query
Access PDO object directly
If you want to use the PDO object directly, it is available as $this->db->pdo. This way you can take full advantage of all of the PDO features, but you must abide by its syntax and rules. Example:
class Members_Model extends TinyMVC_Model { function get_members() { $results = null; try { foreach ($this->db->pdo->query('SELECT * from members') as $row) $results[] = $row; $this->db->pdo = null; } catch (PDOException $e) { trigger_error($e->getMessage()); return false; } return $results; } }
