Posted By

Abe on 11/13/09


Tagged

php codeigniter models


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

dantreacy
jatap
wortell
melik
hifi
grablermassive


How to Write a Better Model in CodeIgniter


 / Published in: PHP
 

URL: http://shawnmccool.com/2009/08/28/how-to-write-a-better-model-in-code-igniter/

  1. <?php if (!defined('BASEPATH')) exit('No direct script access');
  2.  
  3. class Users_model extends Model {
  4. var $table = 'users';
  5.  
  6. function __construct() {
  7. parent::Model();
  8. }
  9.  
  10. function Users_model() {
  11. parent::Model();
  12. }
  13.  
  14. function add_user($options = array()) {
  15. if(!$this->_required(array('email', 'password'), $options))
  16. return false;
  17.  
  18. $options = $this->_default(array('status' => 'active'), $options);
  19.  
  20. $qualificationArray = array('email', 'username', 'status');
  21. foreach($qualificationArray as $qualifier) {
  22. if (isset($options[$qualifier]))
  23. $this->db->set($qualifier, $options[$qualifier]);
  24. }
  25.  
  26. if (isset($options['password']))
  27. $this->db->set('password', md5($options['password']));
  28.  
  29. $this->db->insert($this->table);
  30.  
  31. return $this->db->insert_id();
  32. }
  33.  
  34. function update_user($options = array()) {
  35. if(!$this->_required(array('email'), $options))
  36. return false;
  37.  
  38. $qualificationArray = array('email', 'username', 'status');
  39. foreach($qualificationArray as $qualifier) {
  40. if (isset($options[$qualifier]))
  41. $this->db->set($qualifier, $options[$qualifier]);
  42. }
  43.  
  44. $this->db->where('email', $options['email']);
  45.  
  46. if(isset($options['password']))
  47. $this->db->set('password', md5($options['password']));
  48.  
  49. $this->db->update($this->table);
  50.  
  51. return $this->db->affected_rows();
  52. }
  53.  
  54. function get_users($options = array()) {
  55. $options = $this->_default(array('sort_direction' => 'asc'), $options);
  56.  
  57. $qualificationArray = array('email', 'status');
  58. foreach($qualificationArray as $qualifier) {
  59. if (isset($options[$qualifier]))
  60. $this->db->where($qualifier, $options[$qualifier]);
  61. }
  62.  
  63. if (isset($options['limit']) && isset($options['offset']))
  64. $this->db->limit($options['limit'], $options['offset']);
  65.  
  66. else if (isset($options['limit']))
  67. $this->db->limit($options['limit']);
  68.  
  69. if (isset($options['sort_by']))
  70. $this->db->order_by($options['sort_by'], $options['sort_direction']);
  71.  
  72. $query = $this->db->get($this->table);
  73. if ($query->num_rows() == 0)
  74. return false;
  75.  
  76. if (isset($options['email'])) {
  77. return $query->row(0);
  78.  
  79. } else {
  80. return $query->result();
  81. }
  82. }
  83.  
  84. function delete_user($options = array()) {
  85. if(!$this->_required(array('email'), $options))
  86. return false;
  87.  
  88. $this->db->where('email', $options['email']);
  89. $this->db->delete($this->table);
  90. }
  91.  
  92. function _required($required, $data) {
  93. foreach($required as $field)
  94. if (!isset($data[$field]))
  95. return false;
  96. return true;
  97. }
  98.  
  99. function _default($defaults, $options) {
  100. return array_merge($defaults, $options);
  101. }
  102. }

Report this snippet  

You need to login to post a comment.