RepositoryInterface.php 8.2 KB

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