PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tanora.org/www/framework/system/activerecord.php

https://bitbucket.org/ekkl/tanora
PHP | 265 lines | 151 code | 30 blank | 84 comment | 16 complexity | 3594cad5bc1d8726a6a740ab8e157202 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This is the base class for all active record models.
  4. */
  5. require_once(FRAMEWORK_PATH.'system/model.php');
  6. abstract class ActiveRecord extends Model implements Iterator, Countable{
  7. protected $db;
  8. protected $fields;
  9. protected $table;
  10. protected $query;
  11. /**
  12. * Sets the ORM structure.
  13. */
  14. public function __construct($db = 'default') {
  15. $this->db = Framework::load_database($db);
  16. $this->__structure();
  17. if($this->db instanceof SqlDatabase) {
  18. $this->query = $this->db->query('select');
  19. $this->query->from()->table($this->get_table());
  20. } else {
  21. // NoSQL database
  22. }
  23. }
  24. /**
  25. * Must be defined in subclasses. Define the model structure here
  26. * using the field() method.
  27. */
  28. abstract protected function __structure();
  29. /**
  30. * Loads Field object into the fields member variable and returns
  31. * the reference.
  32. */
  33. protected function _field($name) {
  34. require_once(FRAMEWORK_PATH.'system/field.php');
  35. $dirs = array(FIELD_PATH, FRAMEWORK_PATH.'fields/');
  36. foreach($dirs as $dir) {
  37. $file = $dir.strtolower($name).'field.php';
  38. if(file_exists($file)) {
  39. require_once($file);
  40. $index = count($this->fields);
  41. $class = $name.'Field';
  42. $this->fields[$index] = new $class($this->db);
  43. return $this->fields[$index];
  44. }
  45. }
  46. Framework::error('Cannot load field: '.$name);
  47. }
  48. /**
  49. * Sets the table member variable.
  50. */
  51. protected function _table($name) {
  52. $this->table = $name;
  53. }
  54. /**
  55. * Gets the table member variable.
  56. */
  57. public function get_table() {
  58. return $this->table;
  59. }
  60. /**
  61. * Returns the list of fields as a reference.
  62. */
  63. public function get_fields() {
  64. return $this->fields;
  65. }
  66. /**
  67. * Returns a reference to a single field.
  68. */
  69. public function get_field($name) {
  70. foreach($this->get_fields() as $field) {
  71. if($field->get_name() == $name) {
  72. return $field;
  73. }
  74. }
  75. Framework::error('Field '.$name.' does not exist in '.get_class($this));
  76. }
  77. /**
  78. * Returns a Form object with the appropriate ORM reference.
  79. */
  80. public function form() {
  81. require_once(FRAMEWORK_PATH.'system/form.php');
  82. return new Form($this);
  83. }
  84. /**
  85. * Sets the query member variable and returns the reference.
  86. */
  87. public function query() {
  88. return $this->query;
  89. }
  90. /**
  91. * Loads the row at the specified index.
  92. */
  93. public function load($index) {
  94. $row = $this->query->get($index);
  95. $this->set($row);
  96. }
  97. /**
  98. * Counts the number of rows, from Countable interface.
  99. */
  100. public function count() {
  101. return count($this->query);
  102. }
  103. /**
  104. * Gets the current row.
  105. */
  106. public function current() {
  107. $row = $this->query->current();
  108. $this->set($row);
  109. return $this;
  110. }
  111. /**
  112. * Rewinds.
  113. */
  114. public function rewind() {
  115. $this->query->rewind();
  116. }
  117. /**
  118. * Returns the position.
  119. */
  120. public function key() {
  121. return $this->query->key();
  122. }
  123. /**
  124. * Moves to the next row.
  125. */
  126. public function next() {
  127. $this->query->next();
  128. }
  129. /**
  130. * Checks if row exists.
  131. */
  132. public function valid() {
  133. return $this->query->valid();
  134. }
  135. /**
  136. * Gets the query result.
  137. */
  138. public function fill() {
  139. $result = $this->query->result();
  140. // loads the first result automatically
  141. if(count($this->query) > 0) {
  142. $this->load(0);
  143. }
  144. }
  145. /**
  146. * Sets the Field values with values from an object.
  147. */
  148. public function set($obj) {
  149. foreach($this->get_fields() as $field) {
  150. $name = $field->get_name();
  151. if(isset($obj->$name)) {
  152. $field->set_value($obj->$name);
  153. }
  154. }
  155. }
  156. /**
  157. * Inserts the field values in the database (or overwrites existing
  158. * ones).
  159. */
  160. public function save() {
  161. if($this->db instanceof SqlDatabase) {
  162. $query = $this->db->query('replace');
  163. $query->table($this->get_table());
  164. foreach($this->get_fields() as $field) {
  165. $value = $field->get_column()->get_value();
  166. $query->set($field->get_name(), $value);
  167. }
  168. $query->run();
  169. } else {
  170. // NoSQL database
  171. }
  172. }
  173. /**
  174. * Deletes the value from the database.
  175. */
  176. public function delete() {
  177. if($this->db instanceof SqlDatabase) {
  178. $query = $this->db->query('delete');
  179. $pkey = $this->db->primary_key($this->get_table());
  180. $query->from($this->get_table());
  181. $query->where()->condition($pkey, $this->$pkey);
  182. $query->run();
  183. } else {
  184. // NoSQL database
  185. }
  186. }
  187. /**
  188. * Creates the database table.
  189. */
  190. public function sync() {
  191. if($this->db instanceof SqlDatabase) {
  192. // TODO: don't drop the table
  193. $query = $this->db->query('drop');
  194. $query->table($this->get_table());
  195. $query->if_exists();
  196. $query->run();
  197. $query = $this->db->query('create');
  198. $query->name($this->get_table());
  199. foreach($this->get_fields() as $field) {
  200. $column = $field->get_column();
  201. $column->create($query);
  202. }
  203. $query->run();
  204. } else {
  205. // NoSQL database
  206. }
  207. }
  208. /**
  209. * Returns the value of a field.
  210. */
  211. public function __get($field) {
  212. if(isset($this->$field)) {
  213. $field = $this->get_field($field);
  214. return $field->get_value();
  215. }
  216. }
  217. /**
  218. * Checks if a field exists.
  219. */
  220. public function __isset($field) {
  221. if(!is_null($this->get_field($field))) {
  222. return !is_null($this->get_field($field)->get_value());
  223. }
  224. return FALSE;
  225. }
  226. /**
  227. * Sets the value of a field.
  228. */
  229. public function __set($field, $value) {
  230. if(!is_null($this->get_field($field))) {
  231. $this->get_field($field)->set_value($value);
  232. }
  233. }
  234. }
  235. ?>