Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Created March 6, 2012 23:48
Show Gist options
  • Save AbeEstrada/1989860 to your computer and use it in GitHub Desktop.
Save AbeEstrada/1989860 to your computer and use it in GitHub Desktop.
Snippet: PHP Codeigniter Model
<?php
class Users extends CI_Model {
function add($options=array()) {
// required values
if (!$this->_required(array('email'), $options)) return false;
// default values
$options = $this->_default(array('status'=>'active'), $options);
// qualification (make sure that we're not allowing the site to insert data that it shouldn't)
$qualificationArray = array('username', 'email', 'status');
foreach($qualificationArray as $qualifier) {
if (isset($options[$qualifier]))
$this->db->set($qualifier, $options[$qualifier]);
}
// MD5 the password if it is set
if (isset($options['password']))
$this->db->set('userPassword', md5($options['password']));
// Execute the query
$this->db->insert('users');
// Return the ID of the inserted row, or false if the row could not be inserted
return $this->db->insert_id();
}
function update($options=array()) {
// required values
if (!$this->_required(array('id'), $options))
return false;
// qualification (make sure that we're not allowing the site to update data that it shouldn't)
$qualificationArray = array('username', 'email', 'status');
foreach($qualificationArray as $qualifier) {
if (isset($options[$qualifier]))
$this->db->set($qualifier, $options[$qualifier]);
}
$this->db->where('id', $options['id']);
// MD5 the password if it is set
if (isset($options['password']))
$this->db->set('password', md5($options['password']));
// Execute the query
$this->db->update('users');
// Return the number of rows updated, or false if the row could not be inserted
return $this->db->affected_rows();
}
function get($options=array()) {
// default values
$options = $this->_default(array('sort_direction' => 'asc'), $options);
// Add where clauses to query
$qualificationArray = array('id', 'email', 'status');
foreach($qualificationArray as $qualifier) {
if (isset($options[$qualifier]))
$this->db->where($qualifier, $options[$qualifier]);
}
// If limit / offset are declared (usually for pagination) then we need to take them into account
if (isset($options['limit']) && isset($options['offset']))
$this->db->limit($options['limit'], $options['offset']);
elseif (isset($options['limit']))
$this->db->limit($options['limit']);
// sort
if (isset($options['sort_by']))
$this->db->order_by($options['sort_by'], $options['sort_direction']);
$query = $this->db->get('users');
if ($query->num_rows() == 0) return false;
if (isset($options['id']) && isset($options['email'])) {
// If we know that we're returning a singular record, then let's just return the object
return $query->row(0);
} else {
// If we could be returning any number of records then we'll need to do so as an array of objects
return $query->result();
}
}
function delete($options=array()) {
// required values
if (!$this->_required(array('id'), $options)) return false;
$this->db->where('userId', $options['id']);
$this->db->delete('users');
}
function _default($defaults, $options) {
return array_merge($defaults, $options);
}
function _required($required, $data) {
foreach($required as $field)
if (!isset($data[$field]))
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment