/Release/Last/Framework/Lib/model/base/BaseEntityModel.php

https://github.com/sergiygladkyy/OEF · PHP · 314 lines · 162 code · 63 blank · 89 comment · 18 complexity · 9023fcb203660a64756bc08ac2988bf5 MD5 · raw file

  1. <?php
  2. require_once('lib/model/base/BaseNotStorageEntityModel.php');
  3. abstract class BaseEntityModel extends BaseNotStorageEntityModel
  4. {
  5. /* This entity params */
  6. protected $id = null;
  7. protected $isNew = true;
  8. protected $isDeleted = false;
  9. public function __construct($kind, $type, array& $options = array())
  10. {
  11. parent::__construct($kind, $type, $options);
  12. }
  13. /**
  14. * Initialize entity object (retrieve configuration)
  15. *
  16. * @param string $type - entity name
  17. * @return boolean
  18. */
  19. protected function initialize($kind, $type)
  20. {
  21. // Setup configuration
  22. if (!parent::initialize($kind, $type)) return false;
  23. $this->id = null;
  24. $this->isNew = true;
  25. $this->isDeleted = false;
  26. return true;
  27. }
  28. /**
  29. * Return entity id
  30. *
  31. * @return int
  32. */
  33. public function getId()
  34. {
  35. return $this->isNew ? null : $this->id;
  36. }
  37. /**
  38. * Load entity with id = $id
  39. *
  40. * @param int $id - entity id
  41. * @param array& $options
  42. * @return boolean
  43. */
  44. public function load($id, array& $options = array())
  45. {
  46. if (!is_numeric($id) || $id <= 0) return false;
  47. $id = (int) $id;
  48. $pkey = $this->conf['db_map']['pkey'];
  49. $query = "SELECT * FROM `".$this->conf['db_map']['table']."` WHERE `".$pkey."`=".$id;
  50. $db = $this->container->getDBManager($options);
  51. $values = $db->loadAssoc($query);
  52. if (is_null($values)) return false;
  53. $this->id = $values[$pkey];
  54. unset($values[$pkey]);
  55. $this->attributes = $values;
  56. unset($values);
  57. $this->isNew = false;
  58. $this->isDeleted = false;
  59. $this->isModified = false;
  60. $this->modified = array();
  61. return true;
  62. }
  63. /**
  64. * Set entity params from array
  65. *
  66. * @param array $values
  67. * @param array& $options
  68. * @return array - errors
  69. */
  70. public function fromArray(array $values, array $options = array())
  71. {
  72. if ($errors = $this->prepareToImport($values, $options))
  73. {
  74. return $errors;
  75. }
  76. return parent::fromArray($values);
  77. }
  78. /**
  79. * Prepare object to import from array
  80. *
  81. * @param array& $values
  82. * @param array& $options
  83. * @return array - errors
  84. */
  85. protected function prepareToImport(array& $values, array& $options = array())
  86. {
  87. $errors = array();
  88. $pkey = $this->conf['db_map']['pkey'];
  89. if (!empty($values[$pkey])) // Load
  90. {
  91. if (!$this->load($values[$pkey]))
  92. {
  93. $errors[$pkey] = 'Invalid entity id';
  94. }
  95. }
  96. else // New
  97. {
  98. $this->id = null;
  99. $this->isNew = true;
  100. $this->isDeleted = false;
  101. }
  102. return $errors;
  103. }
  104. /**
  105. * Get entity params as array
  106. * [
  107. * options = array(
  108. * with_link_desc => [ true | false ]
  109. * )
  110. * ]
  111. * @param array& $options
  112. * @return array or null
  113. */
  114. public function toArray(array $options = array())
  115. {
  116. //if ($this->isNew) return array();
  117. $result = parent::toArray($options);
  118. $result[$this->conf['db_map']['pkey']] = $this->id;
  119. return $result;
  120. }
  121. /**
  122. * Save entity
  123. *
  124. * @param array& $options
  125. * @return array - errors
  126. */
  127. public function save(array& $options = array())
  128. {
  129. if ($this->isDeleted) return array('"'.ucfirst($this->type).'" have been deleted');
  130. if (!$this->isModified && !$this->isNew) return array();
  131. $db_map =& $this->conf['db_map'];
  132. // Validation
  133. $errors = $this->validateAttributes(array_keys($this->modified), $options);
  134. if (!empty($errors)) return $errors;
  135. // Save entity
  136. $fields = array_intersect_key($this->attributes, $this->modified);
  137. $db = $this->container->getDBManager($options);
  138. $func = $this->isNew ? 'generateInsertQuery' : 'generateUpdateQuery';
  139. $query = $this->$func($fields, $options);
  140. if (!$db->executeQuery($query))
  141. {
  142. return array($db->getError());
  143. }
  144. if ($this->isNew)
  145. {
  146. $this->id = $db->getInsertId();
  147. $this->isNew = false;
  148. }
  149. $this->modified = array();
  150. $this->isModified = false;
  151. return array();
  152. }
  153. /**
  154. * Generate INSERT SQL query
  155. *
  156. * @param array& $attributes
  157. * @param array& $options
  158. * @return string
  159. */
  160. protected function generateInsertQuery(array& $attributes, array& $options = array())
  161. {
  162. // Attributes
  163. if (list($field, $value) = each($attributes))
  164. {
  165. $fields = "`".$field."`";
  166. $values = $this->getValueForSQL($field, $value);
  167. while (list($field, $value) = each($attributes))
  168. {
  169. $fields .= ", `".$field."`";
  170. $values .= ", ".$this->getValueForSQL($field, $value);
  171. }
  172. }
  173. $query = "INSERT INTO `".$this->conf['db_map']['table']."`(".$fields.") ";
  174. $query .= "VALUES(".$values.")";
  175. return $query;
  176. }
  177. /**
  178. * Generate UPDATE SQL query
  179. *
  180. * @param array& $attributes
  181. * @param array& $options
  182. * @return string
  183. */
  184. protected function generateUpdateQuery(array& $attributes, array& $options = array())
  185. {
  186. $fields = array();
  187. // Attributes
  188. foreach ($attributes as $field => $value)
  189. {
  190. $fields[] = "`".$field."`=".$this->getValueForSQL($field, $value);
  191. }
  192. $db_map =& $this->conf['db_map'];
  193. $query = "UPDATE `".$db_map['table']."` SET ".implode(", ", $fields)." WHERE `".$db_map['pkey']."`=".$this->id;
  194. return $query;
  195. }
  196. /**
  197. * Get value as string to mysql query
  198. *
  199. * @param string $name - attribute name
  200. * @param mixed $value - attribute value
  201. * @return string
  202. */
  203. protected function getValueForSQL($name, $value)
  204. {
  205. switch($this->conf['types'][$name])
  206. {
  207. case 'bool':
  208. case 'int':
  209. case 'float':
  210. case 'reference':
  211. return $value;
  212. break;
  213. case 'enum':
  214. return is_string($value) ? "'".$value."'" : $value;
  215. break;
  216. default:
  217. return "'".$value."'";
  218. }
  219. }
  220. /**
  221. * Delete entity
  222. *
  223. * @param array& $options
  224. * @return array - errors
  225. */
  226. public function delete(array& $options = array())
  227. {
  228. if ($this->isNew) return array();
  229. // Remove this
  230. $errors = $this->removeThis($options);
  231. $this->isDeleted = true;
  232. return $errors;
  233. }
  234. /**
  235. * Remove this entity
  236. *
  237. * @param array& $options
  238. * @return array - errors
  239. */
  240. protected function removeThis(array& $options = array())
  241. {
  242. $db = $this->container->getDBManager($options);
  243. $dbmap =& $this->conf['db_map'];
  244. $query = "DELETE FROM `".$dbmap['table']."` WHERE `".$dbmap['pkey']."`=".$this->id;
  245. if (!$db->executeQuery($query))
  246. {
  247. return array($db->getError());
  248. }
  249. return array();
  250. }
  251. }