RepositoryInterface.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. /**
  17. * Describes the methods that any class representing a data storage should
  18. * comply with.
  19. *
  20. * @method $this setAlias($alias)
  21. * @method string getAlias()
  22. */
  23. interface RepositoryInterface
  24. {
  25. /**
  26. * Returns the table alias or sets a new one
  27. *
  28. * @deprecated 3.4.0 Use setAlias()/getAlias() instead.
  29. * @param string|null $alias the new table alias
  30. * @return string
  31. */
  32. public function alias($alias = null);
  33. /**
  34. * Test to see if a Repository has a specific field/column.
  35. *
  36. * @param string $field The field to check for.
  37. * @return bool True if the field exists, false if it does not.
  38. */
  39. public function hasField($field);
  40. /**
  41. * Creates a new Query for this repository and applies some defaults based on the
  42. * type of search that was selected.
  43. *
  44. * ### Model.beforeFind event
  45. *
  46. * Each find() will trigger a `Model.beforeFind` event for all attached
  47. * listeners. Any listener can set a valid result set using $query
  48. *
  49. * @param string $type the type of query to perform
  50. * @param array|\ArrayAccess $options An array that will be passed to Query::applyOptions()
  51. * @return \Cake\ORM\Query
  52. */
  53. public function find($type = 'all', $options = []);
  54. /**
  55. * Returns a single record after finding it by its primary key, if no record is
  56. * found this method throws an exception.
  57. *
  58. * ### Example:
  59. *
  60. * ```
  61. * $id = 10;
  62. * $article = $articles->get($id);
  63. *
  64. * $article = $articles->get($id, ['contain' => ['Comments]]);
  65. * ```
  66. *
  67. * @param mixed $primaryKey primary key value to find
  68. * @param array|\ArrayAccess $options options accepted by `Table::find()`
  69. * @throws \Cake\Datasource\Exception\RecordNotFoundException if the record with such id
  70. * could not be found
  71. * @return \Cake\Datasource\EntityInterface
  72. * @see \Cake\Datasource\RepositoryInterface::find()
  73. */
  74. public function get($primaryKey, $options = []);
  75. /**
  76. * Creates a new Query instance for this repository
  77. *
  78. * @return \Cake\ORM\Query
  79. */
  80. public function query();
  81. /**
  82. * Update all matching records.
  83. *
  84. * Sets the $fields to the provided values based on $conditions.
  85. * This method will *not* trigger beforeSave/afterSave events. If you need those
  86. * first load a collection of records and update them.
  87. *
  88. * @param string|array|callable|\Cake\Database\Expression\QueryExpression $fields A hash of field => new value.
  89. * @param mixed $conditions Conditions to be used, accepts anything Query::where()
  90. * can take.
  91. * @return int Count Returns the affected rows.
  92. */
  93. public function updateAll($fields, $conditions);
  94. /**
  95. * Deletes all records matching the provided conditions.
  96. *
  97. * This method will *not* trigger beforeDelete/afterDelete events. If you
  98. * need those first load a collection of records and delete them.
  99. *
  100. * This method will *not* execute on associations' `cascade` attribute. You should
  101. * use database foreign keys + ON CASCADE rules if you need cascading deletes combined
  102. * with this method.
  103. *
  104. * @param mixed $conditions Conditions to be used, accepts anything Query::where()
  105. * can take.
  106. * @return int Returns the number of affected rows.
  107. * @see \Cake\Datasource\RepositoryInterface::delete()
  108. */
  109. public function deleteAll($conditions);
  110. /**
  111. * Returns true if there is any record in this repository matching the specified
  112. * conditions.
  113. *
  114. * @param array|\ArrayAccess $conditions list of conditions to pass to the query
  115. * @return bool
  116. */
  117. public function exists($conditions);
  118. /**
  119. * Persists an entity based on the fields that are marked as dirty and
  120. * returns the same entity after a successful save or false in case
  121. * of any error.
  122. *
  123. * @param \Cake\Datasource\EntityInterface $entity the entity to be saved
  124. * @param array|\ArrayAccess $options The options to use when saving.
  125. * @return \Cake\Datasource\EntityInterface|bool
  126. */
  127. public function save(EntityInterface $entity, $options = []);
  128. /**
  129. * Delete a single entity.
  130. *
  131. * Deletes an entity and possibly related associations from the database
  132. * based on the 'dependent' option used when defining the association.
  133. *
  134. * @param \Cake\Datasource\EntityInterface $entity The entity to remove.
  135. * @param array|\ArrayAccess $options The options for the delete.
  136. * @return bool success
  137. */
  138. public function delete(EntityInterface $entity, $options = []);
  139. /**
  140. * Create a new entity + associated entities from an array.
  141. *
  142. * This is most useful when hydrating request data back into entities.
  143. * For example, in your controller code:
  144. *
  145. * ```
  146. * $article = $this->Articles->newEntity($this->request->getData());
  147. * ```
  148. *
  149. * The hydrated entity will correctly do an insert/update based
  150. * on the primary key data existing in the database when the entity
  151. * is saved. Until the entity is saved, it will be a detached record.
  152. *
  153. * @param array|null $data The data to build an entity with.
  154. * @param array $options A list of options for the object hydration.
  155. * @return \Cake\Datasource\EntityInterface
  156. */
  157. public function newEntity($data = null, array $options = []);
  158. /**
  159. * Create a list of entities + associated entities from an array.
  160. *
  161. * This is most useful when hydrating request data back into entities.
  162. * For example, in your controller code:
  163. *
  164. * ```
  165. * $articles = $this->Articles->newEntities($this->request->getData());
  166. * ```
  167. *
  168. * The hydrated entities can then be iterated and saved.
  169. *
  170. * @param array $data The data to build an entity with.
  171. * @param array $options A list of options for the objects hydration.
  172. * @return \Cake\Datasource\EntityInterface[] An array of hydrated records.
  173. */
  174. public function newEntities(array $data, array $options = []);
  175. /**
  176. * Merges the passed `$data` into `$entity` respecting the accessible
  177. * fields configured on the entity. Returns the same entity after being
  178. * altered.
  179. *
  180. * This is most useful when editing an existing entity using request data:
  181. *
  182. * ```
  183. * $article = $this->Articles->patchEntity($article, $this->request->getData());
  184. * ```
  185. *
  186. * @param \Cake\Datasource\EntityInterface $entity the entity that will get the
  187. * data merged in
  188. * @param array $data key value list of fields to be merged into the entity
  189. * @param array $options A list of options for the object hydration.
  190. * @return \Cake\Datasource\EntityInterface
  191. */
  192. public function patchEntity(EntityInterface $entity, array $data, array $options = []);
  193. /**
  194. * Merges each of the elements passed in `$data` into the entities
  195. * found in `$entities` respecting the accessible fields configured on the entities.
  196. * Merging is done by matching the primary key in each of the elements in `$data`
  197. * and `$entities`.
  198. *
  199. * This is most useful when editing a list of existing entities using request data:
  200. *
  201. * ```
  202. * $article = $this->Articles->patchEntities($articles, $this->request->getData());
  203. * ```
  204. *
  205. * @param array|\Traversable $entities the entities that will get the
  206. * data merged in
  207. * @param array $data list of arrays to be merged into the entities
  208. * @param array $options A list of options for the objects hydration.
  209. * @return \Cake\Datasource\EntityInterface[]
  210. */
  211. public function patchEntities($entities, array $data, array $options = []);
  212. }