Posted By

jtkendall on 01/10/09


Tagged

php5 model MVC ci crud codeigniter


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

Folio
Akiva
localhorst
umang_nine
dantreacy
hosefvon
Priestd09


CI CRUD Model


 / Published in: PHP
 

URL: http://doesthismakesense.org/projects/crud/

  1. <?php
  2.  
  3. /**
  4.   * CRUD Model
  5.   * @author Joshua Kendall <[email protected]>
  6.   * @package Models
  7.   * @date 01/10/2009
  8.   * @revision 3
  9.   * @access public
  10.   * @desc CRUD Model for CI sites
  11.   * @doc http://doesthismakesense.org/projects/crud/
  12.   */
  13. class crud_model extends Model
  14. {
  15.  
  16. /**
  17. * Constructor Method
  18. *
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. }
  24.  
  25. /**
  26. * Create
  27. * @param $table
  28. * @param $data
  29. * @param $sql_modifiers
  30. * @return
  31. */
  32. public function create($table, $data = array(), $sql_modifiers = array())
  33. {
  34. foreach($sql_modifiers as $modifier)
  35. {
  36. if(!isset($modifier[2]))
  37. {
  38. $this->db->$modifier[0]($modifier[1]);
  39. } else {
  40. if(!isset($modifier[3]))
  41. {
  42. $this->db->$modifier[0]($modifier[1], $modifier[2]);
  43. } else {
  44. $this->db->$modifier[0]($modifier[1], $modifier[2], $modifier[3]);
  45. }
  46. }
  47. }
  48. $query = $this->db->insert($table, $data);
  49. return;
  50. }
  51.  
  52. /**
  53. * Read
  54. * @param $table
  55. * @param $sql_modifiers
  56. * @return Object
  57. */
  58. public function read($table, $sql_modifiers = array())
  59. {
  60. foreach($sql_modifiers as $modifier)
  61. {
  62. if(!isset($modifier[2]))
  63. {
  64. $this->db->$modifier[0]($modifier[1]);
  65. } else {
  66. if(!isset($modifier[3]))
  67. {
  68. $this->db->$modifier[0]($modifier[1], $modifier[2]);
  69. } else {
  70. $this->db->$modifier[0]($modifier[1], $modifier[2], $modifier[3]);
  71. }
  72. }
  73. }
  74. $query = $this->db->get($table);
  75. return $query->result();
  76. }
  77.  
  78. /**
  79. * Update
  80. * @param $table
  81. * @param $data
  82. * @param $sql_modifiers
  83. * @return
  84. */
  85. public function update($table, $data = array(), $sql_modifiers = array())
  86. {
  87. foreach($sql_modifiers as $modifier)
  88. {
  89. if(!isset($modifier[2]))
  90. {
  91. $this->db->$modifier[0]($modifier[1]);
  92. } else {
  93. if(!isset($modifier[3]))
  94. {
  95. $this->db->$modifier[0]($modifier[1], $modifier[2]);
  96. } else {
  97. $this->db->$modifier[0]($modifier[1], $modifier[2], $modifier[3]);
  98. }
  99. }
  100. }
  101. $query = $this->db->update($table, $data);
  102. }
  103.  
  104. /**
  105. * Delete
  106. * @param $table
  107. * @param $params
  108. * @param $sql_modifiers
  109. * @return
  110. */
  111. public function delete($table, $sql_modifiers = array())
  112. {
  113. foreach($sql_modifiers as $modifier)
  114. {
  115. if(!isset($modifier[2]))
  116. {
  117. $this->db->$modifier[0]($modifier[1]);
  118. } else {
  119. if(!isset($modifier[3]))
  120. {
  121. $this->db->$modifier[0]($modifier[1], $modifier[2]);
  122. } else {
  123. $this->db->$modifier[0]($modifier[1], $modifier[2], $modifier[3]);
  124. }
  125. }
  126. }
  127. $query = $this->db->delete($table);
  128. }
  129.  
  130. /**
  131. * Execute SQL File
  132. * @param $file_name
  133. * @return
  134. */
  135. public function execute_sql_file($file_name)
  136. {
  137. $file = file($file_name);
  138. foreach($file as $query)
  139. {
  140. $result = $this->db->query($query);
  141. }
  142. return;
  143. }
  144.  
  145. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hosefvon on August 6, 2012

I really love this model,. thanks for sharing

Posted By: hosefvon on August 6, 2012

I really love this model,. thanks for sharing

You need to login to post a comment.