Lin_Model.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Extended Model Class
  4. *
  5. * Provides a number of useful functions to generate model specific queries.
  6. * Takes inspiration from CakePHP's implementation of Model and keeps the function
  7. * names pretty same.
  8. *
  9. * A list of functions would be:
  10. *
  11. * - loadTable
  12. * - find
  13. * - findAll
  14. * - findCount
  15. * - field
  16. * - generateList
  17. * - generateSingleArray
  18. * - getAffectedRows
  19. * - getID
  20. * - getInsertID
  21. * - getNumRows
  22. * - insert
  23. * - read
  24. * - save
  25. * - remove
  26. * - query
  27. * - lastQuery
  28. *
  29. * @package CodeIgniter
  30. * @subpackage Libraries
  31. * @category Libraries
  32. * @author Md Emran Hasan (phpfour@gmail.com)
  33. * @link http://phpfour.com
  34. */
  35. class Lin_Model extends CI_Model
  36. {
  37. /**
  38. * Value of the primary key ID of the record that this model is currently pointing to
  39. *
  40. * @public unknown_type
  41. * @access public
  42. */
  43. public $id = null;
  44. /**
  45. * Container for the data that this model gets from persistent storage (the database).
  46. *
  47. * @public array
  48. * @access public
  49. */
  50. public $data = array();
  51. /**
  52. * The name of the associate table name of the Model object
  53. * @public string
  54. * @access public
  55. */
  56. public $_table;
  57. /**
  58. * The name of the ID field for this Model.
  59. *
  60. * @public string
  61. * @access public
  62. */
  63. public $primaryKey = 'id';
  64. /**
  65. * Container for the fields of the table that this model gets from persistent storage (the database).
  66. *
  67. * @public array
  68. * @access public
  69. */
  70. public $fields = array();
  71. /**
  72. * The last inserted ID of the data that this model created
  73. *
  74. * @public int
  75. * @access private
  76. */
  77. public $__insertID = null;
  78. /**
  79. * The number of records returned by the last query
  80. *
  81. * @access private
  82. * @public int
  83. */
  84. public $__numRows = null;
  85. /**
  86. * The number of records affected by the last query
  87. *
  88. * @access private
  89. * @public int
  90. */
  91. public $__affectedRows = null;
  92. /**
  93. * Tells the model whether to return results in array or not
  94. *
  95. * @public string
  96. * @access public
  97. */
  98. public $returnArray = TRUE;
  99. /**
  100. * Prints helpful debug messages if asked
  101. *
  102. * @public string
  103. * @access public
  104. */
  105. public $debug = FALSE;
  106. /**
  107. * Constructor
  108. *
  109. * @access public
  110. */
  111. function __construct()
  112. {
  113. parent::__construct();
  114. log_message('debug', "Extended Model Class Initialized");
  115. }
  116. /**
  117. * Load the associated database table.
  118. *
  119. * @author md emran hasan <emran@rightbrainsolution.com>
  120. * @access public
  121. */
  122. function load_table($table, $fields = array())
  123. {
  124. if ($this->debug) log_message('debug', "Loading model table: $table");
  125. $this->_table = $table;
  126. $this->fields = (!empty($fields)) ? $fields : $this->db->list_fields($table);
  127. if ($this->debug)
  128. {
  129. log_message('debug', "Successfully Loaded model table: $table");
  130. }
  131. }
  132. /**
  133. * Returns a resultset array with specified fields from database matching given conditions.
  134. *
  135. * @author md emran hasan <emran@rightbrainsolution.com>
  136. * @return query result either in array or in object based on model config
  137. * @access public
  138. */
  139. function find_all($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL)
  140. {
  141. if ($conditions != NULL)
  142. {
  143. if(is_array($conditions))
  144. {
  145. $this->db->where($conditions);
  146. }
  147. else
  148. {
  149. $this->db->where($conditions, NULL, FALSE);
  150. }
  151. }
  152. if ($fields != NULL)
  153. {
  154. $this->db->select($fields);
  155. }
  156. if ($order != NULL)
  157. {
  158. if(stripos($order,',') !== false)
  159. {
  160. $orders = explode(',',$order);
  161. foreach ($orders as $v)
  162. {
  163. $this->db->order_by($v);
  164. }
  165. }
  166. else
  167. {
  168. $this->db->order_by($order);
  169. }
  170. }
  171. if ($limit != NULL)
  172. {
  173. $this->db->limit($limit, $start);
  174. }
  175. $query = $this->db->get($this->_table);
  176. $this->__numRows = $query->num_rows();
  177. return ($this->returnArray) ? $query->result_array() : $query->result();
  178. }
  179. function find_zd($conditions = NULL, $pc = NULL , $zd = NULL , $fields = '*', $order = NULL, $start = 0, $limit = NULL)
  180. {
  181. $fieldsToSelect = "$fields, COUNT(CASE WHEN $zd THEN 1 ELSE NULL END) AS zd";
  182. $this->db->select($fields);
  183. if (!empty($conditions))
  184. {
  185. $this->db->where($conditions);
  186. }
  187. $this->db->group_by($pc);
  188. if ($order != NULL)
  189. {
  190. if(stripos($order,',') !== false)
  191. {
  192. $orders = explode(',',$order);
  193. foreach ($orders as $v)
  194. {
  195. $this->db->order_by($v);
  196. }
  197. }
  198. else
  199. {
  200. $this->db->order_by($order);
  201. }
  202. }
  203. if ($limit != NULL)
  204. {
  205. $this->db->limit($limit, $start);
  206. }
  207. $query = $this->db->get($this->_table);
  208. $this->__numRows = $query->num_rows();
  209. return ($this->returnArray) ? $query->result_array() : $query->result();
  210. }
  211. function find_pc($conditions = NULL, $pc = NULL ,$fields = '*', $order = NULL, $start = 0, $limit = NULL)
  212. {
  213. if ($conditions != NULL)
  214. {
  215. if(is_array($conditions))
  216. {
  217. $this->db->where($conditions);
  218. }
  219. else
  220. {
  221. $this->db->where($conditions.' GROUP BY '.$pc, NULL, FALSE);
  222. }
  223. }
  224. if ($fields != NULL)
  225. {
  226. $this->db->select($fields);
  227. }
  228. if ($order != NULL)
  229. {
  230. if(stripos($order,',') !== false)
  231. {
  232. $orders = explode(',',$order);
  233. foreach ($orders as $v)
  234. {
  235. $this->db->order_by($v);
  236. }
  237. }
  238. else
  239. {
  240. $this->db->order_by($order);
  241. }
  242. }
  243. if ($limit != NULL)
  244. {
  245. $this->db->limit($limit, $start);
  246. }
  247. $query = $this->db->get($this->_table);
  248. $this->__numRows = $query->num_rows();
  249. return ($this->returnArray) ? $query->result_array() : $query->result();
  250. }
  251. function paypal($ktime,$jtime,$start = 0, $limit = NULL)
  252. {
  253. $this->db->where("library = 2 and paypal != '' and waybill != '' and librarytime >= '$ktime' and librarytime < '$jtime'", NULL, FALSE);
  254. $this->db->select('orderinfo,number,shop,paypal,waybill,express');
  255. $this->db->order_by('id desc');
  256. $query = $this->db->get($this->_table);
  257. $this->__numRows = $query->num_rows();
  258. return ($this->returnArray) ? $query->result_array() : $query->result();
  259. }
  260. /**
  261. * Return a single row as a resultset array with specified fields from database matching given conditions.
  262. *
  263. * @author md emran hasan <emran@rightbrainsolution.com>
  264. * @return single row either in array or in object based on model config
  265. * @access public
  266. */
  267. function find($conditions = NULL, $fields = '*', $order = NULL)
  268. {
  269. $data = $this->find_all($conditions, $fields, $order, 0, 1);
  270. if ($data)
  271. {
  272. return $data[0];
  273. }
  274. else
  275. {
  276. return false;
  277. }
  278. }
  279. /**
  280. * Returns contents of a field in a query matching given conditions.
  281. *
  282. * @author md emran hasan <emran@rightbrainsolution.com>
  283. * @return string the value of the field specified of the first row
  284. * @access public
  285. */
  286. function field($conditions = null, $name, $fields = '*', $order = NULL)
  287. {
  288. $data = $this->find_all($conditions, $fields, $order, 0, 1);
  289. if ($data)
  290. {
  291. $row = $data[0];
  292. if (isset($row[$name]))
  293. {
  294. return $row[$name];
  295. }
  296. else
  297. {
  298. return false;
  299. }
  300. }
  301. else
  302. {
  303. return false;
  304. }
  305. }
  306. /**
  307. * Returns number of rows matching given SQL condition.
  308. *
  309. * @author md emran hasan <emran@rightbrainsolution.com>
  310. * @return integer the number of records returned by the condition
  311. * @access public
  312. */
  313. function find_count($conditions = null)
  314. {
  315. $data = $this->find_all($conditions, 'COUNT(*) AS count', null, 0, 1);
  316. if ($data)
  317. {
  318. return $data[0]['count'];
  319. }
  320. else
  321. {
  322. return false;
  323. }
  324. }
  325. /**
  326. * Returns a key value pair array from database matching given conditions.
  327. *
  328. * Example use: generateList(null, '', 0. 10, 'id', 'username');
  329. * Returns: array('10' => 'emran', '11' => 'hasan')
  330. *
  331. * @author md emran hasan <emran@rightbrainsolution.com>
  332. * @return array a list of key val ue pairs given criteria
  333. * @access public
  334. */
  335. function generate_list($conditions = null, $order = 'id ASC', $start = 0, $limit = NULL, $key = null, $value = null)
  336. {
  337. $data = $this->find_all($conditions, "$key, $value", $order, $start, $limit);
  338. if ($data)
  339. {
  340. foreach ($data as $row)
  341. {
  342. $keys[] = ($this->returnArray) ? $row[$key] : $row->$key;
  343. $vals[] = ($this->returnArray) ? $row[$value] : $row->$value;
  344. }
  345. if (!empty($keys) && !empty($vals))
  346. {
  347. $return = array_combine($keys, $vals);
  348. return $return;
  349. }
  350. }
  351. else
  352. {
  353. return false;
  354. }
  355. }
  356. /**
  357. * Returns an array of the values of a specific column from database matching given conditions.
  358. *
  359. * Example use: generateSingleArray(null, 'name');
  360. *
  361. * @author md emran hasan <emran@rightbrainsolution.com>
  362. * @return array a list of key value pairs given criteria
  363. * @access public
  364. */
  365. function generate_single_array($conditions = null, $field = null, $order = 'id ASC', $start = 0, $limit = NULL)
  366. {
  367. $data = $this->find_all($conditions, "$field", $order, $start, $limit);
  368. if ($data)
  369. {
  370. foreach ($data as $row)
  371. {
  372. $arr[] = ($this->returnArray) ? $row[$field] : $row->$field;
  373. }
  374. return $arr;
  375. }
  376. else
  377. {
  378. return false;
  379. }
  380. }
  381. /**
  382. * Initializes the model for writing a new record.
  383. *
  384. * @author md emran hasan <emran@rightbrainsolution.com>
  385. * @return boolean True
  386. * @access public
  387. */
  388. function create()
  389. {
  390. $this->id = false;
  391. unset ($this->data);
  392. $this->data = array();
  393. return true;
  394. }
  395. /**
  396. * Returns a list of fields from the database and saves in the model
  397. *
  398. * @author md emran hasan <emran@rightbrainsolution.com>
  399. * @return array Array of database fields
  400. * @access public
  401. */
  402. function read($id = null, $fields = null)
  403. {
  404. if ($id != null)
  405. {
  406. $this->id = $id;
  407. }
  408. $id = $this->id;
  409. if ($this->id !== null && $this->id !== false)
  410. {
  411. $this->data = $this->find($this->primaryKey . ' = ' . $id, $fields);
  412. return $this->data;
  413. }
  414. else
  415. {
  416. return false;
  417. }
  418. }
  419. /**
  420. * Inserts a new record in the database.
  421. *
  422. * @author md emran hasan <emran@rightbrainsolution.com>
  423. * @return boolean success
  424. * @access public
  425. */
  426. function insert($data = null)
  427. {
  428. if ($data == null)
  429. {
  430. return FALSE;
  431. }
  432. $this->data = $data;
  433. $this->data['create_date'] = date("Y-m-d H:i:s");
  434. foreach ($this->data as $key => $value)
  435. {
  436. if (array_search($key, $this->fields) === FALSE)
  437. {
  438. unset($this->data[$key]);
  439. }
  440. }
  441. $this->db->insert($this->_table, $this->data);
  442. $this->__insertID = $this->db->insert_id();
  443. return $this->__insertID;
  444. }
  445. /**
  446. * Saves model data to the database.
  447. *
  448. * @author md emran hasan <emran@rightbrainsolution.com>
  449. * @return boolean success
  450. * @access public
  451. */
  452. function save($data = null, $id = null )
  453. {
  454. if ($data)
  455. {
  456. $this->data = $data;
  457. }
  458. foreach ($this->data as $key => $value)
  459. {
  460. if (array_search($key, $this->fields) === FALSE)
  461. {
  462. unset($this->data[$key]);
  463. }
  464. }
  465. if ($id != null)
  466. {
  467. $this->id = $id;
  468. }
  469. $id = $this->id;
  470. if ($this->id !== null && $this->id !== false)
  471. {
  472. $this->db->where($this->primaryKey, $id);
  473. $this->db->update($this->_table, $this->data);
  474. $this->__affectedRows = $this->db->affected_rows();
  475. return $this->id;
  476. }
  477. else
  478. {
  479. $this->db->insert($this->_table, $this->data);
  480. $this->__insertID = $this->db->insert_id();
  481. return $this->__insertID;
  482. }
  483. }
  484. /**
  485. * Removes record for given id. If no id is given, the current id is used. Returns true on success.
  486. *
  487. * @author md emran hasan <emran@rightbrainsolution.com>
  488. * @return boolean True on success
  489. * @access public
  490. */
  491. function remove($id = null)
  492. {
  493. if ($id != null)
  494. {
  495. $this->id = $id;
  496. }
  497. $id = $this->id;
  498. if ($this->id !== null && $this->id !== false)
  499. {
  500. if ($this->db->delete($this->_table, array($this->primaryKey => $id)))
  501. {
  502. $this->id = null;
  503. $this->data = array();
  504. return true;
  505. }
  506. else
  507. {
  508. return false;
  509. }
  510. }
  511. else
  512. {
  513. return false;
  514. }
  515. }
  516. /**
  517. * Returns a resultset for given SQL statement. Generic SQL queries should be made with this method.
  518. *
  519. * @author md emran hasan <emran@rightbrainsolution.com>
  520. * @return array Resultset
  521. * @access public
  522. */
  523. function query($sql)
  524. {
  525. return $this->db->query($sql);
  526. }
  527. /**
  528. * Returns the last query that was run (the query string, not the result).
  529. *
  530. * @author md emran hasan <emran@rightbrainsolution.com>
  531. * @return string SQL statement
  532. * @access public
  533. */
  534. function last_query()
  535. {
  536. return $this->db->last_query();
  537. }
  538. /**
  539. * This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string.
  540. *
  541. * @author md emran hasan <emran@rightbrainsolution.com>
  542. * @return string SQL statement
  543. * @access public
  544. */
  545. function insert_string($data)
  546. {
  547. return $this->db->insert_string($this->_table, $data);
  548. }
  549. /**
  550. * Returns the current record's ID.
  551. *
  552. * @author md emran hasan <emran@rightbrainsolution.com>
  553. * @return integer The ID of the current record
  554. * @access public
  555. */
  556. function get_ID()
  557. {
  558. return $this->id;
  559. }
  560. /**
  561. * Returns the ID of the last record this Model inserted.
  562. *
  563. * @author md emran hasan <emran@rightbrainsolution.com>
  564. * @return int
  565. * @access public
  566. */
  567. function get_insert_ID()
  568. {
  569. return $this->__insertID;
  570. }
  571. /**
  572. * Returns the number of rows returned from the last query.
  573. *
  574. * @author md emran hasan <emran@rightbrainsolution.com>
  575. * @return int
  576. * @access public
  577. */
  578. function get_num_rows()
  579. {
  580. return $this->__numRows;
  581. }
  582. /**
  583. * Returns the number of rows affected by the last query
  584. *
  585. * @author md emran hasan <emran@rightbrainsolution.com>
  586. * @return int
  587. * @access public
  588. */
  589. function get_affected_rows()
  590. {
  591. return $this->__affectedRows;
  592. }
  593. }
  594. // END Model Class