Entity.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. namespace BBOrm;
  3. abstract class Entity {
  4. const table_name = '';
  5. const connection_name = 'default';
  6. const softdelete = false;
  7. const _ignore = Array(
  8. 'classname',
  9. '_properties',
  10. '_ignore',
  11. 'connection_name',
  12. 'timestamps',
  13. 'softdelete',
  14. 'table_name'
  15. );
  16. const _properties = Array();
  17. public function teste() {
  18. return static::class;
  19. }
  20. public static function get_properties() {
  21. $properties = Array();
  22. foreach (static::_properties as $propertie) {
  23. $obj = new \stdClass();
  24. $obj->data = $propertie;
  25. $properties[] = $obj;
  26. }
  27. return json_encode($properties);
  28. }
  29. function __construct() {
  30. if (isset($this->timestamps) && $this->timestamps) {
  31. $this->created_at = date('Y-m-d h:m:s');
  32. $this->updated_at = date('Y-m-d h:m:s');
  33. }
  34. }
  35. public function __set($property, $value) {
  36. if (in_array($property, static::_ignore)) {
  37. return;
  38. }
  39. $this->$property = $value;
  40. }
  41. public function save() {
  42. if (isset($this->id)) {
  43. $this->update();
  44. } else {
  45. $this->create();
  46. }
  47. }
  48. private function update() {
  49. $updatekeys = static::_ignore;
  50. $updatekeys[] = 'id';
  51. $columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
  52. $obj = (array) $this;
  53. $update_string = '';
  54. $update_data = Array();
  55. $update_data['_update_id'] = $this->id;
  56. foreach ($columns as $column) {
  57. $update_string .= $column . ":$column, ";
  58. $update_data[$column] = $obj[$column];
  59. }
  60. $update_string = rtrim($update_string, ', ');
  61. $sql = "UPDATE " . static::table_name . " SET $update_string WHERE id = :_update_id";
  62. DBInstance::execute($sql, $this->connection_name, $update_data);
  63. }
  64. private function create() {
  65. $columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
  66. $obj = (array) $this;
  67. $first_argument = '';
  68. $second_argument = '';
  69. $insert_data = Array();
  70. foreach ($columns as $column) {
  71. $first_argument .= $column . ', ';
  72. $insert_data[$column] = $obj[$column];
  73. $second_argument .= ":$column, ";
  74. }
  75. $first_argument = rtrim($first_argument, ', ');
  76. $second_argument = rtrim($second_argument, ', ');
  77. $sql = "INSERT INTO " . static::table_name . " ($first_argument) VALUES ($second_argument)";
  78. DBInstance::execute($sql, static::connection_name, $insert_data);
  79. $this->id = DBInstance::last_insert();
  80. }
  81. public function delete() {
  82. if (isset($this->softdelete) && $this->softdelete) {
  83. $this->deleted_at = date('Y-m-d h:m:s');
  84. $this->update();
  85. return;
  86. }
  87. $this->purge();
  88. }
  89. public function load($id = false) {
  90. if (!$id) {
  91. return;
  92. }
  93. $sql = "SELECT * FROM " . static::table_name . " WHERE id = ?";
  94. $this->fill(DBInstance::query_prepare($sql, Array($id), static::connection_name)[0]);
  95. }
  96. public function purge() {
  97. $sql = "DELETE FROM " . static::table_name . " WHERE id = :id";
  98. DBInstance::execute($sql, static::connection_name, Array('id' => $this->id));
  99. }
  100. private function fill($data) {
  101. $data = (array) $data;
  102. foreach ($data as $key => $value) {
  103. $this->$key = $value;
  104. }
  105. }
  106. /**
  107. * Find All
  108. *
  109. * This method will load all instances from the class.
  110. * Limits are avaliable for paginations.
  111. *
  112. * @param Array $select Columns to select. Array('id', 'name')
  113. * @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
  114. * @param boolean $trashed Bring trashed elements?
  115. */
  116. public static function find_all($select = Array('*'), $limits = Array(), $trashed = false) {
  117. $criteria = '';
  118. $limits_sql = '';
  119. foreach ($select as $value) {
  120. $criteria .= $value . ', ';
  121. }
  122. $criteria = rtrim($criteria, ', ');
  123. foreach ($limits as $key => $value) {
  124. $limits_sql .= "$key $value ";
  125. }
  126. if (static::softdelete && !$trashed) {
  127. $sql = "SELECT $criteria FROM " . static::table_name . " WHERE deleted_at is null $limits_sql";
  128. } else {
  129. $sql = "SELECT $criteria FROM " . static::table_name . " $limits_sql";
  130. }
  131. $results = DBInstance::query($sql, static::connection_name);
  132. $objects = Array();
  133. foreach ($results as $value) {
  134. $static = static::class;
  135. $object = new $static;
  136. $object->fill($value);
  137. $objects[] = $object;
  138. }
  139. return $objects;
  140. }
  141. /**
  142. * Count
  143. *
  144. * Count how many records there is on the database
  145. * in relation with this class with the parameters filter
  146. *
  147. *
  148. *
  149. * @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
  150. * @param boolean $trashed Bring trashed registers?
  151. *
  152. */
  153. public static function count($criterias = Array(), $trashed = false) {
  154. $criteria_sql = "";
  155. $criteria_data = Array();
  156. foreach ($criterias as $key => $criteria) {
  157. if ($criteria_sql != "") {
  158. $criteria_sql .= " AND ";
  159. } else {
  160. $criteria_sql .= " WHERE ";
  161. }
  162. if (is_array($criteria[1])) {
  163. $crit_temp = '';
  164. foreach ($criteria[1] as $crit) {
  165. $crit_temp .= '?, ';
  166. $criteria_data[] = $crit;
  167. }
  168. $crit_temp = rtrim($crit_temp, ', ');
  169. $criteria_sql .= "$key $criteria[0] ($crit_temp)";
  170. } else {
  171. $criteria_sql .= "$key $criteria[0] (?) ";
  172. $criteria_data[] = $criteria[1];
  173. }
  174. }
  175. if (static::softdelete && !$trashed) {
  176. $sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql AND deleted_at is null";
  177. } else {
  178. $sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql";
  179. }
  180. $results = DBInstance::query_one($sql, static::connection_name, $criteria_data);
  181. return $results[0]->count;
  182. }
  183. /**
  184. * Find One
  185. *
  186. *
  187. * @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
  188. * @param Array $select Array(id, fullname)
  189. * @param Boolan $trashed true, false
  190. */
  191. public static function find_one($criterias = Array(), $select = Array('*'), $trashed = false) {
  192. $select_sql = "";
  193. $criteria_sql = "";
  194. $limits_sql = "";
  195. $criteria_data = Array();
  196. $limits = Array("LIMIT" => 1);
  197. if (is_numeric($criterias)) {
  198. $criteria_sql = "WHERE id = ? ";
  199. $criteria_data[] = $criterias;
  200. } else {
  201. foreach ($criterias as $key => $criteria) {
  202. if ($criteria_sql != "") {
  203. $criteria_sql .= " AND ";
  204. } else {
  205. $criteria_sql .= " WHERE ";
  206. }
  207. if (is_array($criteria[1])) {
  208. $crit_temp = '';
  209. foreach ($criteria[1] as $crit) {
  210. $crit_temp .= '?, ';
  211. $criteria_data[] = $crit;
  212. }
  213. $crit_temp = rtrim($crit_temp, ', ');
  214. $criteria_sql .= "$key $criteria[0] ($crit_temp)";
  215. } else {
  216. $criteria_sql .= "$key $criteria[0] (?) ";
  217. $criteria_data[] = $criteria[1];
  218. }
  219. }
  220. }
  221. foreach ($select as $value) {
  222. $select_sql .= $value . ', ';
  223. }
  224. $select_sql = rtrim($select_sql, ', ');
  225. foreach ($limits as $key => $value) {
  226. $limits_sql .= "$key $value ";
  227. }
  228. if (static::softdelete && !$trashed) {
  229. $sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql";
  230. } else {
  231. $sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql";
  232. }
  233. $results = DBInstance::query($sql, static::connection_name, $criteria_data);
  234. $objects = Array();
  235. foreach ($results as $value) {
  236. $class = static::class;
  237. $object = new $class;
  238. $object->fill($value);
  239. $objects[] = $object;
  240. }
  241. if (empty($objects)) {
  242. return false; //new $this->classname;
  243. }
  244. return $objects[0];
  245. }
  246. /**
  247. * Find Many
  248. *
  249. * @param Array $criterias Description
  250. * @param Array $select Description
  251. * @param Array $limits Description
  252. * @param boolean $trashed Description
  253. *
  254. */
  255. public static function find_many($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
  256. $select_sql = "";
  257. $criteria_sql = "";
  258. $limits_sql = "";
  259. foreach ($limits as $key => $value) {
  260. $limits_sql .= "$key $value ";
  261. }
  262. foreach ($criterias as $key => $criteria) {
  263. if ($criteria_sql != "") {
  264. $criteria_sql .= " AND ";
  265. } else {
  266. $criteria_sql .= " WHERE ";
  267. }
  268. if (is_string($criteria[1])) {
  269. $criteria_sql .= "$key $criteria[0] '$criteria[1]'";
  270. } else if (is_array($criteria[1])) {
  271. $criteria_sql .= "$key $criteria[0] (" . implode(', ', $criteria[1]) . ")";
  272. } else {
  273. $criteria_sql .= "$key $criteria[0] $criteria[1]";
  274. }
  275. }
  276. foreach ($select as $value) {
  277. $select_sql .= $value . ', ';
  278. }
  279. $select_sql = rtrim($select_sql, ', ');
  280. if (static::softdelete && !$trashed) {
  281. $sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql";
  282. } else {
  283. $sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql";
  284. }
  285. $results = DBInstance::query($sql, static::connection_name);
  286. $objects = Array();
  287. foreach ($results as $value) {
  288. $class = static::class;
  289. $object = new $class;
  290. $object->fill($value);
  291. $objects[] = $object;
  292. }
  293. if (empty($objects)) {
  294. return Array();
  295. }
  296. return $objects;
  297. }
  298. /**
  299. * Has One Local
  300. *
  301. * Defines that, this object has a child an only one,
  302. * object in other class.
  303. *
  304. * Table user = (id, name)
  305. * Table phone = (id, number, userid)
  306. *
  307. * This relation will be created on the User class
  308. * making reference to the Phone class
  309. *
  310. * @param Object $foreign_object Class instance from the remote object
  311. * @param (int, string) $field_in_remote Field in local object matchin the remote id
  312. *
  313. * @return Object Instance of the remote class
  314. *
  315. */
  316. protected function has_one($foreign_object, $field_in_remote) {
  317. $obj = new $foreign_object;
  318. return $obj->find_one(Array($field_in_remote => Array('=', $this->id)));
  319. }
  320. /**
  321. * Has Many
  322. *
  323. * Defines that, this object has many instances of other Object
  324. *
  325. * Table Post = (id, name, content)
  326. * Table Comment = (id, name, content, postid)
  327. *
  328. * This relation will be created on the Post class
  329. * Making reference to the Comment class
  330. *
  331. * @param type $foreign_object Instance of a remote class
  332. * @param (int, string) $field_in_foreign The field to match the local id
  333. *
  334. */
  335. protected function has_many($foreign_object, $field_in_foreign) {
  336. $obj = new $foreign_object;
  337. return $obj->find_many(Array($field_in_foreign => Array('=', $this->id)));
  338. }
  339. /**
  340. * Belongs To
  341. *
  342. * Defines that, this object is part of an other class.
  343. * The local field must match the id of an other class.
  344. *
  345. * Table Post = (id, name, content)
  346. * Table Comment = (id, name, content, postid)
  347. *
  348. * This relation will be created on the comment class
  349. * Making reference to the post class
  350. *
  351. * @param Object $foreign_object Instance of a remote class
  352. * @param (int, String) $local_field Remote field relate to local Object
  353. * @param string $remote_field
  354. */
  355. protected function belongs_to($foreign_object, $local_field, $remote_field = 'id') {
  356. $obj = new $foreign_object;
  357. return $obj->find_one(Array($remote_field => Array('=', $this->$local_field)));
  358. }
  359. /**
  360. * Belongs to Many
  361. *
  362. * Defines that this object is related to many other instances
  363. * of other classes throw a pivot table.
  364. *
  365. * Table Post = (id, name, content)
  366. * Table Tag = (id, name)
  367. * Table Post_Tag = (id, postid, tagid)
  368. *
  369. * This relation will be created in both classes
  370. *
  371. * @param Object $foreign_object Instance of the remote class
  372. * @param string $pivot_table Name of the pivot table
  373. * @param int $local_in_pivot Name of field on the pivot in relation of the local class
  374. * @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
  375. * @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
  376. * @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
  377. */
  378. protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
  379. $obj = new $foreign_object;
  380. $limits_sql = '';
  381. foreach ($remote_limit as $key => $value) {
  382. $limits_sql .= "$key $value ";
  383. }
  384. $sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
  385. $relations = DBInstance::query($sql, static::connection_name);
  386. $ids = Array();
  387. foreach ($relations as $relation) {
  388. $ids[] = $relation->$remote_in_pivot;
  389. }
  390. if (empty($ids)) {
  391. return Array();
  392. }
  393. return $obj->find_many(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit);
  394. }
  395. /**
  396. * Belongs to Many Extended
  397. *
  398. * Defines that this object is related to many other instances
  399. * of other classes throw a pivot table.
  400. * This relation will bring the pivot table with the elements inside
  401. *
  402. * Table Post = (id, name, content)
  403. * Table Tag = (id, name)
  404. * Table Post_Tag = (id, postid, tagid)
  405. *
  406. * This relation will be created in both classes
  407. *
  408. * @param Object $foreign_object Instance of the remote class
  409. * @param string $pivot_table Name of the pivot table
  410. * @param int $local_in_pivot Name of field on the pivot in relation of the local class
  411. * @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
  412. * @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
  413. * @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
  414. */
  415. protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
  416. $obj = new $foreign_object;
  417. $limits_sql = '';
  418. foreach ($pivot_limits as $key => $value) {
  419. $limits_sql .= "$key $value ";
  420. }
  421. $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
  422. $relations = DBInstance::query($sql, static::connection_name);
  423. $objects = Array();
  424. if (empty($relations)) {
  425. return Array();
  426. }
  427. foreach ($relations as $relation) {
  428. $relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter));
  429. $objects[] = $relation;
  430. }
  431. if (empty($objects)) {
  432. return Array();
  433. }
  434. return $objects;
  435. }
  436. }