eloquent.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php namespace System\DB;
  2. abstract class Eloquent {
  3. /**
  4. * Indicates if the model exists in the database.
  5. *
  6. * @var bool
  7. */
  8. public $exists = false;
  9. /**
  10. * The model attributes.
  11. *
  12. * @var array
  13. */
  14. public $attributes = array();
  15. /**
  16. * The model's dirty attributes.
  17. *
  18. * @var array
  19. */
  20. public $dirty = array();
  21. /**
  22. * The model's ignored attributes.
  23. *
  24. * Ignored attributes will not be saved to the database, and
  25. * are primarily used to hold relationships.
  26. *
  27. * @var array
  28. */
  29. public $ignore = array();
  30. /**
  31. * The relationships that should be eagerly loaded.
  32. *
  33. * @var array
  34. */
  35. public $includes = array();
  36. /**
  37. * The relationship type the model is currently resolving.
  38. *
  39. * @var string
  40. */
  41. public $relating;
  42. /**
  43. * The foreign key of the "relating" relationship.
  44. *
  45. * @var string
  46. */
  47. public $relating_key;
  48. /**
  49. * The table name of the model being resolved. Used during many-to-many eager loading.
  50. *
  51. * @var string
  52. */
  53. public $relating_table;
  54. /**
  55. * The model query instance.
  56. *
  57. * @var Query
  58. */
  59. public $query;
  60. /**
  61. * Create a new model instance and set the relationships
  62. * that should be eagerly loaded.
  63. *
  64. * @return mixed
  65. */
  66. public static function with()
  67. {
  68. $model = Eloquent\Factory::make(get_called_class());
  69. $model->includes = func_get_args();
  70. return $model;
  71. }
  72. /**
  73. * Get a model by the primary key.
  74. *
  75. * @param int $id
  76. * @return mixed
  77. */
  78. public static function find($id)
  79. {
  80. return Eloquent\Factory::make(get_called_class())->where('id', '=', $id)->first();
  81. }
  82. /**
  83. * Get an array of models from the database.
  84. *
  85. * @return array
  86. */
  87. private function _get()
  88. {
  89. return Eloquent\Hydrate::from($this);
  90. }
  91. /**
  92. * Get the first model result
  93. *
  94. * @return mixed
  95. */
  96. private function _first()
  97. {
  98. $results = Eloquent\Hydrate::from($this->take(1));
  99. if (count($results) > 0)
  100. {
  101. reset($results);
  102. return current($results);
  103. }
  104. }
  105. /**
  106. * Retrieve the query for a 1:1 relationship.
  107. *
  108. * @param string $model
  109. * @return mixed
  110. */
  111. public function has_one($model)
  112. {
  113. return Eloquent\Relate::has_one($model, $this);
  114. }
  115. /**
  116. * Retrieve the query for a 1:* relationship.
  117. *
  118. * @param string $model
  119. * @return mixed
  120. */
  121. public function has_many($model)
  122. {
  123. return Eloquent\Relate::has_many($model, $this);
  124. }
  125. /**
  126. * Retrieve the query for a 1:1 belonging relationship.
  127. *
  128. * @param string $model
  129. * @return mixed
  130. */
  131. public function belongs_to($model)
  132. {
  133. // -----------------------------------------------------
  134. // Get the calling function name.
  135. // -----------------------------------------------------
  136. list(, $caller) = debug_backtrace(false);
  137. return Eloquent\Relate::belongs_to($caller, $model, $this);
  138. }
  139. /**
  140. * Retrieve the query for a *:* relationship.
  141. *
  142. * @param string $model
  143. * @return mixed
  144. */
  145. public function has_many_and_belongs_to($model)
  146. {
  147. return Eloquent\Relate::has_many_and_belongs_to($model, $this);
  148. }
  149. /**
  150. * Save the model to the database.
  151. *
  152. * @return bool
  153. */
  154. public function save()
  155. {
  156. if ($this->exists and count($this->dirty) == 0)
  157. {
  158. return true;
  159. }
  160. return Eloquent\Warehouse::store($this);
  161. }
  162. /**
  163. * Magic method for retrieving model attributes.
  164. */
  165. public function __get($key)
  166. {
  167. // -----------------------------------------------------
  168. // Check the ignored attributes first.
  169. // -----------------------------------------------------
  170. if (array_key_exists($key, $this->ignore))
  171. {
  172. return $this->ignore[$key];
  173. }
  174. // -----------------------------------------------------
  175. // Is the attribute actually a relationship?
  176. // -----------------------------------------------------
  177. if (method_exists($this, $key))
  178. {
  179. // -----------------------------------------------------
  180. // Get the query / model for the relationship.
  181. // -----------------------------------------------------
  182. $model = $this->$key();
  183. return ($this->relating == 'has_one' or $this->relating == 'belongs_to')
  184. ? $this->ignore[$key] = $model->first()
  185. : $this->ignore[$key] = $model->get();
  186. }
  187. return (array_key_exists($key, $this->attributes)) ? $this->attributes[$key] : null;
  188. }
  189. /**
  190. * Magic Method for setting model attributes.
  191. */
  192. public function __set($key, $value)
  193. {
  194. // -----------------------------------------------------
  195. // Is the key actually a relationship?
  196. // -----------------------------------------------------
  197. if (method_exists($this, $key))
  198. {
  199. $this->ignore[$key] = $value;
  200. }
  201. else
  202. {
  203. $this->attributes[$key] = $value;
  204. $this->dirty[$key] = $value;
  205. }
  206. }
  207. /**
  208. * Magic Method for determining if a model attribute is set.
  209. */
  210. public function __isset($key)
  211. {
  212. return (array_key_exists($key, $this->attributes) or array_key_exists($key, $this->ignore));
  213. }
  214. /**
  215. * Magic Method for unsetting model attributes.
  216. */
  217. public function __unset($key)
  218. {
  219. unset($this->attributes[$key]);
  220. unset($this->ignore[$key]);
  221. unset($this->dirty[$key]);
  222. }
  223. /**
  224. * Magic Method for handling dynamic method calls.
  225. */
  226. public function __call($method, $parameters)
  227. {
  228. if ($method == 'get')
  229. {
  230. return $this->_get();
  231. }
  232. if ($method == 'first')
  233. {
  234. return $this->_first();
  235. }
  236. // -----------------------------------------------------
  237. // If the method is an aggregate function, just return
  238. // the aggregate value from the query.
  239. // -----------------------------------------------------
  240. if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))
  241. {
  242. return call_user_func_array(array($this->query, $method), $parameters);
  243. }
  244. // -----------------------------------------------------
  245. // Pass the method call to the query instance.
  246. // -----------------------------------------------------
  247. call_user_func_array(array($this->query, $method), $parameters);
  248. return $this;
  249. }
  250. /**
  251. * Magic Method for handling dynamic static method calls.
  252. */
  253. public static function __callStatic($method, $parameters)
  254. {
  255. $model = Eloquent\Factory::make(get_called_class());
  256. if ($method == 'get')
  257. {
  258. return $model->_get();
  259. }
  260. if ($method == 'first')
  261. {
  262. return $model->_first();
  263. }
  264. // -----------------------------------------------------
  265. // If the method is an aggregate function, just return
  266. // the aggregate value from the query.
  267. // -----------------------------------------------------
  268. if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))
  269. {
  270. return call_user_func_array(array($model->query, $method), $parameters);
  271. }
  272. // -----------------------------------------------------
  273. // Pass the method call to the query instance.
  274. // -----------------------------------------------------
  275. call_user_func_array(array($model->query, $method), $parameters);
  276. return $model;
  277. }
  278. }