PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ManaPHP/Mvc/ModelInterface.php

https://gitlab.com/szlongshu/manaphp
PHP | 266 lines | 31 code | 26 blank | 209 comment | 0 complexity | f8eaa9e3b5aa1ab17fc3e8922c9b3f7d MD5 | raw file
  1. <?php
  2. namespace ManaPHP\Mvc {
  3. /**
  4. * ManaPHP\Mvc\ModelInterface initializer
  5. */
  6. interface ModelInterface
  7. {
  8. /**
  9. * Returns table name mapped in the model
  10. * <code>
  11. * $city->getSource();
  12. * </code>
  13. *
  14. * @return string
  15. */
  16. public function getSource();
  17. /**
  18. * Sets both read/write connection services
  19. *
  20. * @param string $connectionService
  21. *
  22. * @return static
  23. */
  24. public function setConnectionService($connectionService);
  25. /**
  26. * Sets the DependencyInjection connection service used to write data
  27. *
  28. * @param string $connectionService
  29. *
  30. * @return static
  31. */
  32. public function setWriteConnectionService($connectionService);
  33. /**
  34. * Sets the DependencyInjection connection service used to read data
  35. *
  36. * @param string $connectionService
  37. *
  38. * @return static
  39. */
  40. public function setReadConnectionService($connectionService);
  41. /**
  42. * Returns DependencyInjection connection service used to read data
  43. *
  44. * @return string
  45. */
  46. public function getReadConnectionService();
  47. /**
  48. * Returns DependencyInjection connection service used to write data
  49. *
  50. * @return string
  51. */
  52. public function getWriteConnectionService();
  53. /**
  54. * Gets internal database connection
  55. *
  56. * @return \ManaPHP\DbInterface
  57. */
  58. public function getReadConnection();
  59. /**
  60. * Gets internal database connection
  61. *
  62. * @return \ManaPHP\DbInterface
  63. */
  64. public function getWriteConnection();
  65. /**
  66. * Assigns values to a model from an array
  67. * <code>
  68. * $city->assign(['city_id'=>1,'city_name'=>'beijing']);
  69. * $city->assign(['city_id'=>1,'city_name'=>'beijing'],['city_name']);
  70. * </code>
  71. *
  72. * @param array $data
  73. * @param array $whiteList
  74. *
  75. * @return static
  76. */
  77. public function assign($data, $whiteList = null);
  78. /**
  79. * Allows to query a set of records that match the specified conditions
  80. *
  81. * <code>
  82. * $cities=City::find(['country_id'=>2]);
  83. * $cities=City::find(['conditions'=>['country_id'=>2],'order'=>'city_id desc']);
  84. * $cities=City::find([['country_id'=>2],'order'=>'city_id desc']);
  85. * $cities=City::find(['conditions'=>'country_id =:country_id','bind'=>['country_id'=>2]]);
  86. *
  87. * </code>
  88. * @param array $parameters
  89. * @param array $cacheOptions
  90. *
  91. * @return static[]|false
  92. */
  93. public static function find($parameters = null, $cacheOptions = null);
  94. /**
  95. * Allows to query the first record that match the specified conditions
  96. *
  97. * <code>
  98. * $city=City::findFirst(10);
  99. * $city=City::findFirst(['city_id'=>10]);
  100. * $city=City::findFirst(['conditions'=>['city_id'=>10]]);
  101. * $city=City::findFirst(['conditions'=>'city_id =:city_id','bind'=>['city_id'=>10]]);
  102. * </code>
  103. *
  104. * @param string|array $parameters
  105. * @param array $cacheOptions
  106. *
  107. * @return static|false
  108. */
  109. public static function findFirst($parameters = null, $cacheOptions = null);
  110. /**
  111. * Create a criteria for a special model
  112. *
  113. * @param \ManaPHP\DiInterface $dependencyInjector
  114. *
  115. * @return \ManaPHP\Mvc\Model\Query\BuilderInterface
  116. */
  117. public static function query($dependencyInjector = null);
  118. /**
  119. * Allows to count how many records match the specified conditions
  120. *
  121. * <code>
  122. * City::count(['country_id'=>2]);
  123. * </code>
  124. *
  125. * @param array $parameters
  126. * @param string $column
  127. * @param array $cacheOptions
  128. *
  129. * @return int|array
  130. */
  131. public static function count($parameters = null, $column = '*', $cacheOptions = null);
  132. /**
  133. * Allows to calculate a summary on a column that match the specified conditions
  134. *
  135. * @param string $column
  136. * @param array $parameters
  137. * @param array $cacheOptions
  138. *
  139. * @return mixed
  140. */
  141. public static function sum($column, $parameters = null, $cacheOptions = null);
  142. /**
  143. * Allows to get the max value of a column that match the specified conditions
  144. *
  145. * @param string $column
  146. * @param array $parameters
  147. * @param array $cacheOptions
  148. *
  149. * @return mixed
  150. */
  151. public static function max($column, $parameters = null, $cacheOptions = null);
  152. /**
  153. * Allows to get the min value of a column that match the specified conditions
  154. *
  155. * @param string $column
  156. * @param array $parameters
  157. * @param array $cacheOptions =null
  158. *
  159. * @return mixed
  160. */
  161. public static function min($column, $parameters = null, $cacheOptions = null);
  162. /**
  163. * Allows to calculate the average value on a column matching the specified conditions
  164. *
  165. * @param string $column
  166. * @param array $parameters
  167. * @param array $cacheOptions
  168. *
  169. * @return double
  170. */
  171. public static function average($column, $parameters = null, $cacheOptions = null);
  172. /**
  173. * Inserts or updates a model instance. Returning true on success or false otherwise.
  174. *
  175. * @param array $data
  176. * @param array $whiteList
  177. *
  178. * @return boolean
  179. */
  180. public function save($data = null, $whiteList = null);
  181. /**
  182. * Inserts a model instance. If the instance already exists in the persistence it will throw an exception
  183. * Returning true on success or false otherwise.
  184. *
  185. * @param array $data
  186. * @param array $whiteList
  187. *
  188. * @return boolean
  189. */
  190. public function create($data = null, $whiteList = null);
  191. /**
  192. * Updates a model instance. If the instance does n't exist in the persistence it will throw an exception
  193. * Returning true on success or false otherwise.
  194. *
  195. * @param array $data
  196. * @param array $whiteList
  197. *
  198. * @return boolean
  199. */
  200. public function update($data = null, $whiteList = null);
  201. /**
  202. * Deletes a model instance. Returning true on success or false otherwise.
  203. *
  204. * @return boolean
  205. */
  206. public function delete();
  207. /**
  208. * Returns the instance as an array representation
  209. *
  210. *<code>
  211. * print_r($robot->toArray());
  212. *</code>
  213. *
  214. * @return array
  215. */
  216. public function toArray();
  217. /**
  218. * Returns the internal snapshot data
  219. *
  220. * @return array
  221. */
  222. public function getSnapshotData();
  223. /**
  224. * Returns a list of changed values
  225. *
  226. * @return array
  227. * @throws \ManaPHP\Mvc\Model\Exception|\ManaPHP\Di\Exception
  228. */
  229. public function getChangedFields();
  230. /**
  231. * Check if a specific attribute has changed
  232. * This only works if the model is keeping data snapshots
  233. *
  234. * @param string|array $fields
  235. *
  236. * @return bool
  237. */
  238. public function hasChanged($fields);
  239. }
  240. }