123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Extended Model Class
- *
- * Provides a number of useful functions to generate model specific queries.
- * Takes inspiration from CakePHP's implementation of Model and keeps the function
- * names pretty same.
- *
- * A list of functions would be:
- *
- * - loadTable
- * - find
- * - findAll
- * - findCount
- * - field
- * - generateList
- * - generateSingleArray
- * - getAffectedRows
- * - getID
- * - getInsertID
- * - getNumRows
- * - insert
- * - read
- * - save
- * - remove
- * - query
- * - lastQuery
- *
- * @package CodeIgniter
- * @subpackage Libraries
- * @category Libraries
- * @author Md Emran Hasan (phpfour@gmail.com)
- * @link http://phpfour.com
- */
- class Lin_Model extends CI_Model
- {
- /**
- * Value of the primary key ID of the record that this model is currently pointing to
- *
- * @public unknown_type
- * @access public
- */
- public $id = null;
- /**
- * Container for the data that this model gets from persistent storage (the database).
- *
- * @public array
- * @access public
- */
- public $data = array();
- /**
- * The name of the associate table name of the Model object
- * @public string
- * @access public
- */
- public $_table;
- /**
- * The name of the ID field for this Model.
- *
- * @public string
- * @access public
- */
- public $primaryKey = 'id';
- /**
- * Container for the fields of the table that this model gets from persistent storage (the database).
- *
- * @public array
- * @access public
- */
- public $fields = array();
- /**
- * The last inserted ID of the data that this model created
- *
- * @public int
- * @access private
- */
- public $__insertID = null;
- /**
- * The number of records returned by the last query
- *
- * @access private
- * @public int
- */
- public $__numRows = null;
- /**
- * The number of records affected by the last query
- *
- * @access private
- * @public int
- */
- public $__affectedRows = null;
- /**
- * Tells the model whether to return results in array or not
- *
- * @public string
- * @access public
- */
- public $returnArray = TRUE;
- /**
- * Prints helpful debug messages if asked
- *
- * @public string
- * @access public
- */
- public $debug = FALSE;
- /**
- * Constructor
- *
- * @access public
- */
- function __construct()
- {
- parent::__construct();
- log_message('debug', "Extended Model Class Initialized");
- }
- /**
- * Load the associated database table.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @access public
- */
- function load_table($table, $fields = array())
- {
- if ($this->debug) log_message('debug', "Loading model table: $table");
- $this->_table = $table;
- $this->fields = (!empty($fields)) ? $fields : $this->db->list_fields($table);
- if ($this->debug)
- {
- log_message('debug', "Successfully Loaded model table: $table");
- }
- }
- /**
- * Returns a resultset array with specified fields from database matching given conditions.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return query result either in array or in object based on model config
- * @access public
- */
- function find_all($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL)
- {
- if ($conditions != NULL)
- {
- if(is_array($conditions))
- {
- $this->db->where($conditions);
- }
- else
- {
- $this->db->where($conditions, NULL, FALSE);
- }
- }
- if ($fields != NULL)
- {
- $this->db->select($fields);
- }
- if ($order != NULL)
- {
- if(stripos($order,',') !== false)
- {
- $orders = explode(',',$order);
- foreach ($orders as $v)
- {
- $this->db->order_by($v);
- }
- }
- else
- {
- $this->db->order_by($order);
- }
- }
- if ($limit != NULL)
- {
- $this->db->limit($limit, $start);
- }
- $query = $this->db->get($this->_table);
- $this->__numRows = $query->num_rows();
- return ($this->returnArray) ? $query->result_array() : $query->result();
- }
-
- function find_zd($conditions = NULL, $pc = NULL , $zd = NULL , $fields = '*', $order = NULL, $start = 0, $limit = NULL)
- {
- $fieldsToSelect = "$fields, COUNT(CASE WHEN $zd THEN 1 ELSE NULL END) AS zd";
- $this->db->select($fields);
-
- if (!empty($conditions))
- {
- $this->db->where($conditions);
- }
- $this->db->group_by($pc);
- if ($order != NULL)
- {
- if(stripos($order,',') !== false)
- {
- $orders = explode(',',$order);
- foreach ($orders as $v)
- {
- $this->db->order_by($v);
- }
- }
- else
- {
- $this->db->order_by($order);
- }
- }
- if ($limit != NULL)
- {
- $this->db->limit($limit, $start);
- }
- $query = $this->db->get($this->_table);
- $this->__numRows = $query->num_rows();
- return ($this->returnArray) ? $query->result_array() : $query->result();
- }
-
- function find_pc($conditions = NULL, $pc = NULL ,$fields = '*', $order = NULL, $start = 0, $limit = NULL)
- {
- if ($conditions != NULL)
- {
- if(is_array($conditions))
- {
- $this->db->where($conditions);
- }
- else
- {
- $this->db->where($conditions.' GROUP BY '.$pc, NULL, FALSE);
- }
- }
- if ($fields != NULL)
- {
- $this->db->select($fields);
- }
- if ($order != NULL)
- {
- if(stripos($order,',') !== false)
- {
- $orders = explode(',',$order);
- foreach ($orders as $v)
- {
- $this->db->order_by($v);
- }
- }
- else
- {
- $this->db->order_by($order);
- }
- }
- if ($limit != NULL)
- {
- $this->db->limit($limit, $start);
- }
- $query = $this->db->get($this->_table);
- $this->__numRows = $query->num_rows();
- return ($this->returnArray) ? $query->result_array() : $query->result();
- }
-
- function paypal($ktime,$jtime,$start = 0, $limit = NULL)
- {
- $this->db->where("library = 2 and paypal != '' and waybill != '' and librarytime >= '$ktime' and librarytime < '$jtime'", NULL, FALSE);
- $this->db->select('orderinfo,number,shop,paypal,waybill,express');
- $this->db->order_by('id desc');
- $query = $this->db->get($this->_table);
- $this->__numRows = $query->num_rows();
- return ($this->returnArray) ? $query->result_array() : $query->result();
- }
- /**
- * Return a single row as a resultset array with specified fields from database matching given conditions.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return single row either in array or in object based on model config
- * @access public
- */
- function find($conditions = NULL, $fields = '*', $order = NULL)
- {
- $data = $this->find_all($conditions, $fields, $order, 0, 1);
- if ($data)
- {
- return $data[0];
- }
- else
- {
- return false;
- }
- }
- /**
- * Returns contents of a field in a query matching given conditions.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return string the value of the field specified of the first row
- * @access public
- */
- function field($conditions = null, $name, $fields = '*', $order = NULL)
- {
- $data = $this->find_all($conditions, $fields, $order, 0, 1);
- if ($data)
- {
- $row = $data[0];
- if (isset($row[$name]))
- {
- return $row[$name];
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /**
- * Returns number of rows matching given SQL condition.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return integer the number of records returned by the condition
- * @access public
- */
- function find_count($conditions = null)
- {
- $data = $this->find_all($conditions, 'COUNT(*) AS count', null, 0, 1);
- if ($data)
- {
- return $data[0]['count'];
- }
- else
- {
- return false;
- }
- }
- /**
- * Returns a key value pair array from database matching given conditions.
- *
- * Example use: generateList(null, '', 0. 10, 'id', 'username');
- * Returns: array('10' => 'emran', '11' => 'hasan')
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return array a list of key val ue pairs given criteria
- * @access public
- */
- function generate_list($conditions = null, $order = 'id ASC', $start = 0, $limit = NULL, $key = null, $value = null)
- {
- $data = $this->find_all($conditions, "$key, $value", $order, $start, $limit);
- if ($data)
- {
- foreach ($data as $row)
- {
- $keys[] = ($this->returnArray) ? $row[$key] : $row->$key;
- $vals[] = ($this->returnArray) ? $row[$value] : $row->$value;
- }
- if (!empty($keys) && !empty($vals))
- {
- $return = array_combine($keys, $vals);
- return $return;
- }
- }
- else
- {
- return false;
- }
- }
- /**
- * Returns an array of the values of a specific column from database matching given conditions.
- *
- * Example use: generateSingleArray(null, 'name');
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return array a list of key value pairs given criteria
- * @access public
- */
- function generate_single_array($conditions = null, $field = null, $order = 'id ASC', $start = 0, $limit = NULL)
- {
- $data = $this->find_all($conditions, "$field", $order, $start, $limit);
- if ($data)
- {
- foreach ($data as $row)
- {
- $arr[] = ($this->returnArray) ? $row[$field] : $row->$field;
- }
- return $arr;
- }
- else
- {
- return false;
- }
- }
- /**
- * Initializes the model for writing a new record.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return boolean True
- * @access public
- */
- function create()
- {
- $this->id = false;
- unset ($this->data);
- $this->data = array();
- return true;
- }
- /**
- * Returns a list of fields from the database and saves in the model
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return array Array of database fields
- * @access public
- */
- function read($id = null, $fields = null)
- {
- if ($id != null)
- {
- $this->id = $id;
- }
- $id = $this->id;
- if ($this->id !== null && $this->id !== false)
- {
- $this->data = $this->find($this->primaryKey . ' = ' . $id, $fields);
- return $this->data;
- }
- else
- {
- return false;
- }
- }
- /**
- * Inserts a new record in the database.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return boolean success
- * @access public
- */
- function insert($data = null)
- {
- if ($data == null)
- {
- return FALSE;
- }
- $this->data = $data;
- $this->data['create_date'] = date("Y-m-d H:i:s");
- foreach ($this->data as $key => $value)
- {
- if (array_search($key, $this->fields) === FALSE)
- {
- unset($this->data[$key]);
- }
- }
- $this->db->insert($this->_table, $this->data);
- $this->__insertID = $this->db->insert_id();
- return $this->__insertID;
- }
- /**
- * Saves model data to the database.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return boolean success
- * @access public
- */
- function save($data = null, $id = null )
- {
- if ($data)
- {
- $this->data = $data;
- }
-
- foreach ($this->data as $key => $value)
- {
- if (array_search($key, $this->fields) === FALSE)
- {
- unset($this->data[$key]);
- }
- }
- if ($id != null)
- {
- $this->id = $id;
- }
- $id = $this->id;
-
- if ($this->id !== null && $this->id !== false)
- {
- $this->db->where($this->primaryKey, $id);
- $this->db->update($this->_table, $this->data);
- $this->__affectedRows = $this->db->affected_rows();
- return $this->id;
- }
- else
- {
- $this->db->insert($this->_table, $this->data);
- $this->__insertID = $this->db->insert_id();
- return $this->__insertID;
- }
- }
- /**
- * Removes record for given id. If no id is given, the current id is used. Returns true on success.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return boolean True on success
- * @access public
- */
- function remove($id = null)
- {
- if ($id != null)
- {
- $this->id = $id;
- }
- $id = $this->id;
- if ($this->id !== null && $this->id !== false)
- {
- if ($this->db->delete($this->_table, array($this->primaryKey => $id)))
- {
- $this->id = null;
- $this->data = array();
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /**
- * Returns a resultset for given SQL statement. Generic SQL queries should be made with this method.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return array Resultset
- * @access public
- */
- function query($sql)
- {
- return $this->db->query($sql);
- }
- /**
- * Returns the last query that was run (the query string, not the result).
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return string SQL statement
- * @access public
- */
- function last_query()
- {
- return $this->db->last_query();
- }
- /**
- * This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return string SQL statement
- * @access public
- */
- function insert_string($data)
- {
- return $this->db->insert_string($this->_table, $data);
- }
- /**
- * Returns the current record's ID.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return integer The ID of the current record
- * @access public
- */
- function get_ID()
- {
- return $this->id;
- }
- /**
- * Returns the ID of the last record this Model inserted.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return int
- * @access public
- */
- function get_insert_ID()
- {
- return $this->__insertID;
- }
- /**
- * Returns the number of rows returned from the last query.
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return int
- * @access public
- */
- function get_num_rows()
- {
- return $this->__numRows;
- }
- /**
- * Returns the number of rows affected by the last query
- *
- * @author md emran hasan <emran@rightbrainsolution.com>
- * @return int
- * @access public
- */
- function get_affected_rows()
- {
- return $this->__affectedRows;
- }
- }
- // END Model Class
|