orm.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\models;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Description of ORMHelper
  6. *
  7. * @author Michal Czech <michael@modulesgarden.com>
  8. */
  9. class orm extends base {
  10. static $tableStructure;
  11. static function factory($id = false,array $data = array()) {
  12. $class = get_called_class();
  13. return new $class($id, $data);
  14. }
  15. static function getTableStructure($force = false)
  16. {
  17. $class = get_called_class();
  18. if(!empty(static::$tableStructure[$class]) && $force === false)
  19. {
  20. return static::$tableStructure[$class];
  21. }
  22. static::$tableStructure[$class] = array(
  23. 'prefixed' => true
  24. );
  25. $rc = new \ReflectionClass(get_called_class());
  26. if(preg_match("/@Table\((.*)\)/D", $rc->getDocComment(),$result))
  27. {
  28. foreach(explode(',',$result[1]) as $setting)
  29. {
  30. $tmp = explode('=',$setting);
  31. if(isset($tmp[1]))
  32. {
  33. if($tmp[1] == 'false') $value = false;
  34. elseif($tmp[1] == 'true') $value = true;
  35. else $value = $tmp[1];
  36. static::$tableStructure[$class][$tmp[0]] = $value;
  37. }
  38. else
  39. {
  40. static::$tableStructure[$class][$tmp[0]] = true;
  41. }
  42. }
  43. }
  44. foreach($rc->getProperties() as $property)
  45. {
  46. $configs = array();
  47. if(preg_match("/@Column\((.*)\)/D", $property->getDocComment(),$result))
  48. {
  49. foreach(explode(',',$result[1]) as $setting)
  50. {
  51. $tmp = explode('=',$setting);
  52. if(isset($tmp[1]))
  53. {
  54. if($tmp[1] == 'false') $configs[$tmp[0]] = false;
  55. elseif($tmp[1] == 'true') $configs[$tmp[0]] = true;
  56. else $configs[$tmp[0]] = $tmp[1];
  57. }
  58. else
  59. {
  60. $configs[$tmp[0]] = false;
  61. }
  62. }
  63. }
  64. if($configs)
  65. {
  66. if(!isset($configs['as']))
  67. {
  68. $configs['as'] = $property->name;
  69. }
  70. if(!isset($configs['name']))
  71. {
  72. $configs['name'] = preg_replace_callback(
  73. '/([A-Z])/D'
  74. ,function($params){
  75. return '_'.strtolower($params[0]);
  76. }
  77. ,$property->name);
  78. }
  79. static::$tableStructure[$class]['columns'][$property->name] = $configs;
  80. }
  81. $configs = array();
  82. if(preg_match("/@Validation\((.*)\)/D", $property->getDocComment(),$result))
  83. {
  84. foreach(explode(',',$result[1]) as $setting)
  85. {
  86. $tmp = explode('=',$setting);
  87. if(isset($tmp[1]))
  88. {
  89. if($tmp[1] == 'false') $configs[$tmp[0]] = false;
  90. elseif($tmp[1] == 'true') $configs[$tmp[0]] = true;
  91. else $configs[$tmp[0]] = $tmp[1];
  92. }
  93. else
  94. {
  95. $configs[$tmp[0]] = true;
  96. }
  97. }
  98. }
  99. if($configs)
  100. {
  101. static::$tableStructure[$class]['validation'][$property->name] = $configs;
  102. }
  103. }
  104. return static::$tableStructure[$class];
  105. }
  106. static function tableName($prefixed = true){
  107. $tableStructure = self::getTableStructure();
  108. if($prefixed && $tableStructure['prefixed'])
  109. {
  110. return self::prefixTable($tableStructure['name']);
  111. }
  112. else
  113. {
  114. return $tableStructure['name'];
  115. }
  116. }
  117. static function fieldDeclaration(){
  118. $tableStructure = self::getTableStructure();
  119. $output = array();
  120. foreach($tableStructure['columns'] as $column)
  121. {
  122. $output[$column['name']] = $column['as'];
  123. }
  124. return $output;
  125. }
  126. static function getProperyColumn($property){
  127. $tableStructure = self::getTableStructure();
  128. if(!isset($tableStructure['columns'][$property]['name']))
  129. {
  130. throw new main\mgLibs\exceptions\system("Property $property not exists");
  131. }
  132. return $tableStructure['columns'][$property]['name'];
  133. }
  134. protected function getRawData($id){
  135. $data = main\mgLibs\MySQL\query::select(
  136. self::fieldDeclaration()
  137. , self::tableName()
  138. , array(
  139. 'id' => $id
  140. )
  141. )->fetch();
  142. if(empty($data))
  143. {
  144. throw new main\mgLibs\exceptions\system('Unable to find Item with ID:'.$id);
  145. }
  146. return $data;
  147. }
  148. function __construct($id = false,array $data = array()) {
  149. if($id !== false && empty($data))
  150. {
  151. $data = $this->getRawData($id);
  152. }
  153. if(!empty($data))
  154. {
  155. $this->fillProperties($data);
  156. }
  157. }
  158. function save($data = array()){
  159. foreach(self::fieldDeclaration() as $mysqlName => $field)
  160. {
  161. if(isset($data[$field]))continue;
  162. if(is_numeric($mysqlName))
  163. {
  164. $data[$field] = $this->{$field};
  165. }
  166. else
  167. {
  168. $data[$mysqlName] = $this->{$field};
  169. }
  170. }
  171. if($this->id)
  172. {
  173. main\mgLibs\MySQL\query::update(
  174. self::tableName()
  175. , $data
  176. , array(
  177. 'id' => $this->id
  178. )
  179. );
  180. }
  181. else
  182. {
  183. $this->id = main\mgLibs\MySQL\query::insert(
  184. self::tableName()
  185. , $data
  186. );
  187. }
  188. }
  189. /** Alias */
  190. function remove() {
  191. $this->delete();
  192. }
  193. function delete()
  194. {
  195. if($this->id)
  196. {
  197. main\mgLibs\MySQL\query::delete
  198. (
  199. self::tableName(),
  200. array(
  201. 'id' => $this->id
  202. )
  203. );
  204. }
  205. }
  206. function validate(){
  207. $structure = self::getTableStructure();
  208. $errors = array();
  209. foreach($structure['validation'] as $property => $config)
  210. {
  211. if($property == 'id')
  212. {
  213. continue;
  214. }
  215. if(isset($config['notEmpty']) && empty($this->{$property}))
  216. {
  217. $errors[$property][] = 'emptyField';
  218. }
  219. }
  220. if($errors)
  221. {
  222. throw new main\mgLibs\exceptions\validation('validateError',$errors);
  223. }
  224. }
  225. static function prefixTable($table)
  226. {
  227. return main\mgLibs\process\mainInstance::I()->configuration()->tablePrefix.$table;
  228. }
  229. /**
  230. * Fill Current Model Properties
  231. *
  232. * @author Michal Czech <michael@modulesgarden.com>
  233. * @param array $data
  234. */
  235. function fillProperties($data){
  236. if(empty($data))
  237. {
  238. return;
  239. }
  240. $used = array('id'=>true);
  241. if(is_array($data))
  242. {
  243. foreach($data as $property => $value)
  244. {
  245. if(property_exists($this, $property))
  246. {
  247. $this->$property = $value;
  248. $used[$property] = true;
  249. }
  250. }
  251. }
  252. $structure = self::getTableStructure();
  253. foreach($structure['columns'] as $property => $configs)
  254. {
  255. if(!isset($used[$property]) && !isset($configs['notRequired']))
  256. {
  257. throw new main\mgLibs\exceptions\system('Missing object property: '.$property, main\mgLibs\exceptions\codes::MISING_OBJECT_PROPERTY);
  258. }
  259. }
  260. }
  261. }