Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if(!defined('BASEPATH')) { header('Status: 404 Not Found'); exit(); }
- class MY_Model extends CI_Model {
- public $_select = array();
- public $_join = array();
- public $_where = array();
- public $_like = array();
- public $_limit = NULL;
- public $_offset = NULL;
- public $_order_by = NULL;
- public $_order = NULL;
- public $_group_by = NULL;
- public $_having = array();
- protected $_hooks;
- protected $response = NULL;
- public function __construct()
- {
- parent::__construct();
- if ( ! empty($this->models))
- {
- foreach ($this->models as $model)
- {
- $this->load->model($model);
- }
- }
- }
- public function select($select)
- {
- $this->trigger_events('select');
- $this->_select[] = $select;
- return $this;
- }
- public function join($join, $on, $type = NULL)
- {
- $this->trigger_events('join');
- if (!is_array($join))
- {
- $join = array($join, $on, $type);
- }
- array_push($this->_join, $join);
- return $this;
- }
- public function where($where, $value = NULL)
- {
- $this->trigger_events('where');
- if (!is_array($where))
- {
- $where = array($where => $value);
- }
- array_push($this->_where, $where);
- return $this;
- }
- public function like($like, $value = NULL)
- {
- $this->trigger_events('like');
- if (!is_array($like))
- {
- $like = array($like => $value);
- }
- array_push($this->_like, $like);
- return $this;
- }
- public function limit($limit, $offset = NULL)
- {
- $this->trigger_events('limit');
- $this->_limit = $limit;
- if($offset != NULL)
- {
- $this->trigger_events('offset');
- $this->_offset = $offset;
- }
- return $this;
- }
- public function order_by($by, $order='desc')
- {
- $this->trigger_events('order_by');
- $this->_order_by = $by;
- $this->_order = $order;
- return $this;
- }
- public function group_by($by)
- {
- $this->trigger_events('group_by');
- $this->_group_by = $by;
- return $this;
- }
- public function having($having, $value = NULL)
- {
- $this->trigger_events('having');
- if (!is_array($having))
- {
- $having = array($having => $value);
- }
- array_push($this->_having, $having);
- return $this;
- }
- public function row()
- {
- $this->trigger_events('row');
- $row = $this->response->row();
- $this->response->free_result();
- return $row;
- }
- public function result()
- {
- $this->trigger_events('result');
- $result = $this->response->result();
- $this->response->free_result();
- return $result;
- }
- public function num_rows()
- {
- $this->trigger_events(array('num_rows'));
- $result = $this->response->num_rows();
- $this->response->free_result();
- return $result;
- }
- public function get_list() {
- if (isset($this->_select) && !empty($this->_select))
- {
- foreach ($this->_select as $select)
- {
- $this->db->select($select);
- }
- $this->_select = array();
- }
- else
- {
- $this->db->select($this->_default_select);
- }
- if (isset($this->_join) && !empty($this->_join))
- {
- foreach ($this->_join as $join)
- {
- $this->db->join($join[0], $join[1], $join[2]);
- }
- $this->_join = array();
- }
- else
- {
- $this->db->join($this->_default_join[0], $this->_default_join[1], $this->_default_join[2]);
- }
- $this->trigger_events('extra_where');
- if (isset($this->_where) && !empty($this->_where))
- {
- foreach ($this->_where as $where)
- {
- $this->db->where($where);
- }
- $this->_where = array();
- }
- if (isset($this->_like) && !empty($this->_like))
- {
- foreach ($this->_like as $like)
- {
- $this->db->or_like($like);
- }
- $this->_like = array();
- }
- if (isset($this->_limit) && isset($this->_offset))
- {
- $this->db->limit($this->_limit, $this->_offset);
- $this->_limit = NULL;
- $this->_offset = NULL;
- }
- else if (isset($this->_limit))
- {
- $this->db->limit($this->_limit);
- $this->_limit = NULL;
- }
- if (isset($this->_order_by) && isset($this->_order))
- {
- $this->db->order_by($this->_order_by, $this->_order);
- $this->_order = NULL;
- $this->_order_by = NULL;
- }
- if (isset($this->_group_by))
- {
- $this->db->group_by($this->_group_by);
- $this->_group_by = NULL;
- }
- if (isset($this->_having) && !empty($this->_having))
- {
- foreach ($this->_having as $having)
- {
- $this->db->having($having);
- }
- $this->_having = array();
- }
- $this->response = $this->db->get($this->table);
- return $this;
- }
- /**
- * Hooks
- **/
- public function set_hook($event, $name, $class, $method, $arguments)
- {
- $this->_hooks->{$event}[$name] = new stdClass;
- $this->_hooks->{$event}[$name]->class = $class;
- $this->_hooks->{$event}[$name]->method = $method;
- $this->_hooks->{$event}[$name]->arguments = $arguments;
- }
- public function remove_hook($event, $name)
- {
- if (isset($this->_hooks->{$event}[$name]))
- {
- unset($this->_hooks->{$event}[$name]);
- }
- }
- public function remove_hooks($event)
- {
- if (isset($this->_hooks->$event))
- {
- unset($this->_hooks->$event);
- }
- }
- protected function _call_hook($event, $name)
- {
- if (isset($this->_hooks->{$event}[$name]) && method_exists($this->_hooks->{$event}[$name]->class, $this->_hooks->{$event}[$name]->method))
- {
- $hook = $this->_hooks->{$event}[$name];
- return call_user_func_array(array($hook->class, $hook->method), $hook->arguments);
- }
- return FALSE;
- }
- public function trigger_events($events)
- {
- if (is_array($events) && !empty($events))
- {
- foreach ($events as $event)
- {
- $this->trigger_events($event);
- }
- }
- else
- {
- if (isset($this->_hooks->$events) && !empty($this->_hooks->$events))
- {
- foreach ($this->_hooks->$events as $name => $hook)
- {
- $this->_call_hook($events, $name);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement