Table.php 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\ORM;
  16. use ArrayObject;
  17. use BadMethodCallException;
  18. use Cake\Core\App;
  19. use Cake\Database\Schema\Table as Schema;
  20. use Cake\Database\Type;
  21. use Cake\Datasource\EntityInterface;
  22. use Cake\Datasource\Exception\InvalidPrimaryKeyException;
  23. use Cake\Datasource\RepositoryInterface;
  24. use Cake\Event\EventListenerInterface;
  25. use Cake\Event\EventManager;
  26. use Cake\Event\EventManagerTrait;
  27. use Cake\ORM\AssociationCollection;
  28. use Cake\ORM\Association\BelongsTo;
  29. use Cake\ORM\Association\BelongsToMany;
  30. use Cake\ORM\Association\HasMany;
  31. use Cake\ORM\Association\HasOne;
  32. use Cake\ORM\BehaviorRegistry;
  33. use Cake\ORM\Exception\MissingEntityException;
  34. use Cake\ORM\Marshaller;
  35. use Cake\ORM\RulesChecker;
  36. use Cake\ORM\Rule\IsUnique;
  37. use Cake\Utility\Inflector;
  38. use Cake\Validation\Validator;
  39. use RuntimeException;
  40. /**
  41. * Represents a single database table.
  42. *
  43. * Exposes methods for retrieving data out of it, and manages the associations
  44. * this table has to other tables. Multiple instances of this class can be created
  45. * for the same database table with different aliases, this allows you to address
  46. * your database structure in a richer and more expressive way.
  47. *
  48. * ### Retrieving data
  49. *
  50. * The primary way to retrieve data is using Table::find(). See that method
  51. * for more information.
  52. *
  53. * ### Dynamic finders
  54. *
  55. * In addition to the standard find($type) finder methods, CakePHP provides dynamic
  56. * finder methods. These methods allow you to easily set basic conditions up. For example
  57. * to filter users by username you would call
  58. *
  59. * ```
  60. * $query = $users->findByUsername('mark');
  61. * ```
  62. *
  63. * You can also combine conditions on multiple fields using either `Or` or `And`:
  64. *
  65. * ```
  66. * $query = $users->findByUsernameOrEmail('mark', 'mark@example.org');
  67. * ```
  68. *
  69. * ### Bulk updates/deletes
  70. *
  71. * You can use Table::updateAll() and Table::deleteAll() to do bulk updates/deletes.
  72. * You should be aware that events will *not* be fired for bulk updates/deletes.
  73. *
  74. * ### Callbacks/events
  75. *
  76. * Table objects provide a few callbacks/events you can hook into to augment/replace
  77. * find operations. Each event uses the standard event subsystem in CakePHP
  78. *
  79. * - `beforeFind(Event $event, Query $query, ArrayObject $options, boolean $primary)`
  80. * Fired before each find operation. By stopping the event and supplying a
  81. * return value you can bypass the find operation entirely. Any changes done
  82. * to the $query instance will be retained for the rest of the find. The
  83. * $primary parameter indicates whether or not this is the root query,
  84. * or an associated query.
  85. *
  86. * - `buildValidator(Event $event, Validator $validator, string $name)`
  87. * Allows listeners to modify validation rules for the provided named validator.
  88. *
  89. * - `buildRules(Event $event, RulesChecker $rules)`
  90. * Allows listeners to modify the rules checker by adding more rules.
  91. *
  92. * - `beforeRules(Event $event, Entity $entity, ArrayObject $options, string $operation)`
  93. * Fired before an entity is validated using the rules checker. By stopping this event,
  94. * you can return the final value of the rules checking operation.
  95. *
  96. * - `afterRules(Event $event, Entity $entity, ArrayObject $options, bool $result, string $operation)`
  97. * Fired after the rules have been checked on the entity. By stopping this event,
  98. * you can return the final value of the rules checking operation.
  99. *
  100. * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
  101. * Fired before each entity is saved. Stopping this event will abort the save
  102. * operation. When the event is stopped the result of the event will be returned.
  103. *
  104. * - `afterSave(Event $event, Entity $entity, ArrayObject $options)`
  105. * Fired after an entity is saved.
  106. *
  107. * - `beforeDelete(Event $event, Entity $entity, ArrayObject $options)`
  108. * Fired before an entity is deleted. By stopping this event you will abort
  109. * the delete operation.
  110. *
  111. * - `afterDelete(Event $event, Entity $entity, ArrayObject $options)`
  112. * Fired after an entity has been deleted.
  113. *
  114. * @see \Cake\Event\EventManager for reference on the events system.
  115. */
  116. class Table implements RepositoryInterface, EventListenerInterface
  117. {
  118. use EventManagerTrait;
  119. /**
  120. * Name of default validation set.
  121. *
  122. * @var string
  123. */
  124. const DEFAULT_VALIDATOR = 'default';
  125. /**
  126. * Name of the table as it can be found in the database
  127. *
  128. * @var string
  129. */
  130. protected $_table;
  131. /**
  132. * Human name giving to this particular instance. Multiple objects representing
  133. * the same database table can exist by using different aliases.
  134. *
  135. * @var string
  136. */
  137. protected $_alias;
  138. /**
  139. * Connection instance
  140. *
  141. * @var \Cake\Database\Connection
  142. */
  143. protected $_connection;
  144. /**
  145. * The schema object containing a description of this table fields
  146. *
  147. * @var \Cake\Database\Schema\Table
  148. */
  149. protected $_schema;
  150. /**
  151. * The name of the field that represents the primary key in the table
  152. *
  153. * @var string|array
  154. */
  155. protected $_primaryKey;
  156. /**
  157. * The name of the field that represents a human readable representation of a row
  158. *
  159. * @var string
  160. */
  161. protected $_displayField;
  162. /**
  163. * The associations container for this Table.
  164. *
  165. * @var \Cake\ORM\AssociationCollection
  166. */
  167. protected $_associations;
  168. /**
  169. * BehaviorRegistry for this table
  170. *
  171. * @var \Cake\ORM\BehaviorRegistry
  172. */
  173. protected $_behaviors;
  174. /**
  175. * The name of the class that represent a single row for this table
  176. *
  177. * @var string
  178. */
  179. protected $_entityClass;
  180. /**
  181. * Registry key used to create this table object
  182. *
  183. * @var string
  184. */
  185. protected $_registryAlias;
  186. /**
  187. * A list of validation objects indexed by name
  188. *
  189. * @var array
  190. */
  191. protected $_validators = [];
  192. /**
  193. * The domain rules to be applied to entities saved by this table
  194. *
  195. * @var \Cake\ORM\RulesChecker
  196. */
  197. protected $_rulesChecker;
  198. /**
  199. * Initializes a new instance
  200. *
  201. * The $config array understands the following keys:
  202. *
  203. * - table: Name of the database table to represent
  204. * - alias: Alias to be assigned to this table (default to table name)
  205. * - connection: The connection instance to use
  206. * - entityClass: The fully namespaced class name of the entity class that will
  207. * represent rows in this table.
  208. * - schema: A \Cake\Database\Schema\Table object or an array that can be
  209. * passed to it.
  210. * - eventManager: An instance of an event manager to use for internal events
  211. * - behaviors: A BehaviorRegistry. Generally not used outside of tests.
  212. * - associations: An AssociationCollection instance.
  213. * - validator: A Validator instance which is assigned as the "default"
  214. * validation set, or an associative array, where key is the name of the
  215. * validation set and value the Validator instance.
  216. *
  217. * @param array $config List of options for this table
  218. */
  219. public function __construct(array $config = [])
  220. {
  221. if (!empty($config['registryAlias'])) {
  222. $this->registryAlias($config['registryAlias']);
  223. }
  224. if (!empty($config['table'])) {
  225. $this->table($config['table']);
  226. }
  227. if (!empty($config['alias'])) {
  228. $this->alias($config['alias']);
  229. }
  230. if (!empty($config['connection'])) {
  231. $this->connection($config['connection']);
  232. }
  233. if (!empty($config['schema'])) {
  234. $this->schema($config['schema']);
  235. }
  236. if (!empty($config['entityClass'])) {
  237. $this->entityClass($config['entityClass']);
  238. }
  239. $eventManager = $behaviors = $associations = null;
  240. if (!empty($config['eventManager'])) {
  241. $eventManager = $config['eventManager'];
  242. }
  243. if (!empty($config['behaviors'])) {
  244. $behaviors = $config['behaviors'];
  245. }
  246. if (!empty($config['associations'])) {
  247. $associations = $config['associations'];
  248. }
  249. if (!empty($config['validator'])) {
  250. if (!is_array($config['validator'])) {
  251. $this->validator(static::DEFAULT_VALIDATOR, $config['validator']);
  252. } else {
  253. foreach ($config['validator'] as $name => $validator) {
  254. $this->validator($name, $validator);
  255. }
  256. }
  257. }
  258. $this->_eventManager = $eventManager ?: new EventManager();
  259. $this->_behaviors = $behaviors ?: new BehaviorRegistry($this);
  260. $this->_associations = $associations ?: new AssociationCollection();
  261. $this->initialize($config);
  262. $this->_eventManager->on($this);
  263. $this->dispatchEvent('Model.initialize');
  264. }
  265. /**
  266. * Get the default connection name.
  267. *
  268. * This method is used to get the fallback connection name if an
  269. * instance is created through the TableRegistry without a connection.
  270. *
  271. * @return string
  272. * @see \Cake\ORM\TableRegistry::get()
  273. */
  274. public static function defaultConnectionName()
  275. {
  276. return 'default';
  277. }
  278. /**
  279. * Initialize a table instance. Called after the constructor.
  280. *
  281. * You can use this method to define associations, attach behaviors
  282. * define validation and do any other initialization logic you need.
  283. *
  284. * ```
  285. * public function initialize(array $config)
  286. * {
  287. * $this->belongsTo('Users');
  288. * $this->belongsToMany('Tagging.Tags');
  289. * $this->primaryKey('something_else');
  290. * }
  291. * ```
  292. *
  293. * @param array $config Configuration options passed to the constructor
  294. * @return void
  295. */
  296. public function initialize(array $config)
  297. {
  298. }
  299. /**
  300. * Returns the database table name or sets a new one
  301. *
  302. * @param string|null $table the new table name
  303. * @return string
  304. */
  305. public function table($table = null)
  306. {
  307. if ($table !== null) {
  308. $this->_table = $table;
  309. }
  310. if ($this->_table === null) {
  311. $table = namespaceSplit(get_class($this));
  312. $table = substr(end($table), 0, -5);
  313. if (empty($table)) {
  314. $table = $this->alias();
  315. }
  316. $this->_table = Inflector::underscore($table);
  317. }
  318. return $this->_table;
  319. }
  320. /**
  321. * Returns the table alias or sets a new one
  322. *
  323. * @param string|null $alias the new table alias
  324. * @return string
  325. */
  326. public function alias($alias = null)
  327. {
  328. if ($alias !== null) {
  329. $this->_alias = $alias;
  330. }
  331. if ($this->_alias === null) {
  332. $alias = namespaceSplit(get_class($this));
  333. $alias = substr(end($alias), 0, -5) ?: $this->_table;
  334. $this->_alias = $alias;
  335. }
  336. return $this->_alias;
  337. }
  338. /**
  339. * Returns the table registry key used to create this table instance
  340. *
  341. * @param string|null $registryAlias the key used to access this object
  342. * @return string
  343. */
  344. public function registryAlias($registryAlias = null)
  345. {
  346. if ($registryAlias !== null) {
  347. $this->_registryAlias = $registryAlias;
  348. }
  349. if ($this->_registryAlias === null) {
  350. $this->_registryAlias = $this->alias();
  351. }
  352. return $this->_registryAlias;
  353. }
  354. /**
  355. * Returns the connection instance or sets a new one
  356. *
  357. * @param \Cake\Database\Connection|null $conn The new connection instance
  358. * @return \Cake\Database\Connection
  359. */
  360. public function connection($conn = null)
  361. {
  362. if ($conn === null) {
  363. return $this->_connection;
  364. }
  365. return $this->_connection = $conn;
  366. }
  367. /**
  368. * Returns the schema table object describing this table's properties.
  369. *
  370. * If an \Cake\Database\Schema\Table is passed, it will be used for this table
  371. * instead of the default one.
  372. *
  373. * If an array is passed, a new \Cake\Database\Schema\Table will be constructed
  374. * out of it and used as the schema for this table.
  375. *
  376. * @param array|\Cake\Database\Schema\Table|null $schema New schema to be used for this table
  377. * @return \Cake\Database\Schema\Table
  378. */
  379. public function schema($schema = null)
  380. {
  381. if ($schema === null) {
  382. if ($this->_schema === null) {
  383. $this->_schema = $this->_initializeSchema(
  384. $this->connection()
  385. ->schemaCollection()
  386. ->describe($this->table())
  387. );
  388. }
  389. return $this->_schema;
  390. }
  391. if (is_array($schema)) {
  392. $constraints = [];
  393. if (isset($schema['_constraints'])) {
  394. $constraints = $schema['_constraints'];
  395. unset($schema['_constraints']);
  396. }
  397. $schema = new Schema($this->table(), $schema);
  398. foreach ($constraints as $name => $value) {
  399. $schema->addConstraint($name, $value);
  400. }
  401. }
  402. return $this->_schema = $schema;
  403. }
  404. /**
  405. * Override this function in order to alter the schema used by this table.
  406. * This function is only called after fetching the schema out of the database.
  407. * If you wish to provide your own schema to this table without touching the
  408. * database, you can override schema() or inject the definitions though that
  409. * method.
  410. *
  411. * ### Example:
  412. *
  413. * ```
  414. * protected function _initializeSchema(\Cake\Database\Schema\Table $table) {
  415. * $table->columnType('preferences', 'json');
  416. * return $table;
  417. * }
  418. * ```
  419. *
  420. * @param \Cake\Database\Schema\Table $table The table definition fetched from database.
  421. * @return \Cake\Database\Schema\Table the altered schema
  422. * @api
  423. */
  424. protected function _initializeSchema(Schema $table)
  425. {
  426. return $table;
  427. }
  428. /**
  429. * Test to see if a Table has a specific field/column.
  430. *
  431. * Delegates to the schema object and checks for column presence
  432. * using the Schema\Table instance.
  433. *
  434. * @param string $field The field to check for.
  435. * @return bool True if the field exists, false if it does not.
  436. */
  437. public function hasField($field)
  438. {
  439. $schema = $this->schema();
  440. return $schema->column($field) !== null;
  441. }
  442. /**
  443. * Returns the primary key field name or sets a new one
  444. *
  445. * @param string|array|null $key sets a new name to be used as primary key
  446. * @return string|array
  447. */
  448. public function primaryKey($key = null)
  449. {
  450. if ($key !== null) {
  451. $this->_primaryKey = $key;
  452. }
  453. if ($this->_primaryKey === null) {
  454. $key = (array)$this->schema()->primaryKey();
  455. if (count($key) === 1) {
  456. $key = $key[0];
  457. }
  458. $this->_primaryKey = $key;
  459. }
  460. return $this->_primaryKey;
  461. }
  462. /**
  463. * Returns the display field or sets a new one
  464. *
  465. * @param string|null $key sets a new name to be used as display field
  466. * @return string
  467. */
  468. public function displayField($key = null)
  469. {
  470. if ($key !== null) {
  471. $this->_displayField = $key;
  472. }
  473. if ($this->_displayField === null) {
  474. $schema = $this->schema();
  475. $primary = (array)$this->primaryKey();
  476. $this->_displayField = array_shift($primary);
  477. if ($schema->column('title')) {
  478. $this->_displayField = 'title';
  479. }
  480. if ($schema->column('name')) {
  481. $this->_displayField = 'name';
  482. }
  483. }
  484. return $this->_displayField;
  485. }
  486. /**
  487. * Returns the class used to hydrate rows for this table or sets
  488. * a new one
  489. *
  490. * @param string|null $name the name of the class to use
  491. * @throws \Cake\ORM\Exception\MissingEntityException when the entity class cannot be found
  492. * @return string
  493. */
  494. public function entityClass($name = null)
  495. {
  496. if ($name === null && !$this->_entityClass) {
  497. $default = '\Cake\ORM\Entity';
  498. $self = get_called_class();
  499. $parts = explode('\\', $self);
  500. if ($self === __CLASS__ || count($parts) < 3) {
  501. return $this->_entityClass = $default;
  502. }
  503. $alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
  504. $name = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $alias;
  505. if (!class_exists($name)) {
  506. return $this->_entityClass = $default;
  507. }
  508. }
  509. if ($name !== null) {
  510. $class = App::className($name, 'Model/Entity');
  511. $this->_entityClass = $class;
  512. }
  513. if (!$this->_entityClass) {
  514. throw new MissingEntityException([$name]);
  515. }
  516. return $this->_entityClass;
  517. }
  518. /**
  519. * Add a behavior.
  520. *
  521. * Adds a behavior to this table's behavior collection. Behaviors
  522. * provide an easy way to create horizontally re-usable features
  523. * that can provide trait like functionality, and allow for events
  524. * to be listened to.
  525. *
  526. * Example:
  527. *
  528. * Load a behavior, with some settings.
  529. *
  530. * ```
  531. * $this->addBehavior('Tree', ['parent' => 'parentId']);
  532. * ```
  533. *
  534. * Behaviors are generally loaded during Table::initialize().
  535. *
  536. * @param string $name The name of the behavior. Can be a short class reference.
  537. * @param array $options The options for the behavior to use.
  538. * @return void
  539. * @throws \RuntimeException If a behavior is being reloaded.
  540. * @see \Cake\ORM\Behavior
  541. */
  542. public function addBehavior($name, array $options = [])
  543. {
  544. $this->_behaviors->load($name, $options);
  545. }
  546. /**
  547. * Removes a behavior from this table's behavior registry.
  548. *
  549. * Example:
  550. *
  551. * Remove a behavior from this table.
  552. *
  553. * ```
  554. * $this->removeBehavior('Tree');
  555. * ```
  556. *
  557. * @param string $name The alias that the behavior was added with.
  558. * @return void
  559. * @see \Cake\ORM\Behavior
  560. */
  561. public function removeBehavior($name)
  562. {
  563. $this->_behaviors->unload($name);
  564. }
  565. /**
  566. * Returns the behavior registry for this table.
  567. *
  568. * @return \Cake\ORM\BehaviorRegistry
  569. */
  570. public function behaviors()
  571. {
  572. return $this->_behaviors;
  573. }
  574. /**
  575. * Check if a behavior with the given alias has been loaded.
  576. *
  577. * @param string $name The behavior alias to check.
  578. * @return bool
  579. */
  580. public function hasBehavior($name)
  581. {
  582. return $this->_behaviors->has($name);
  583. }
  584. /**
  585. * Returns an association object configured for the specified alias if any
  586. *
  587. * @param string $name the alias used for the association
  588. * @return \Cake\ORM\Association
  589. */
  590. public function association($name)
  591. {
  592. return $this->_associations->get($name);
  593. }
  594. /**
  595. * Get the associations collection for this table.
  596. *
  597. * @return \Cake\ORM\AssociationCollection
  598. */
  599. public function associations()
  600. {
  601. return $this->_associations;
  602. }
  603. /**
  604. * Setup multiple associations.
  605. *
  606. * It takes an array containing set of table names indexed by association type
  607. * as argument:
  608. *
  609. * ```
  610. * $this->Posts->addAssociations([
  611. * 'belongsTo' => [
  612. * 'Users' => ['className' => 'App\Model\Table\UsersTable']
  613. * ],
  614. * 'hasMany' => ['Comments'],
  615. * 'belongsToMany' => ['Tags']
  616. * ]);
  617. * ```
  618. *
  619. * Each association type accepts multiple associations where the keys
  620. * are the aliases, and the values are association config data. If numeric
  621. * keys are used the values will be treated as association aliases.
  622. *
  623. * @param array $params Set of associations to bind (indexed by association type)
  624. * @return void
  625. * @see \Cake\ORM\Table::belongsTo()
  626. * @see \Cake\ORM\Table::hasOne()
  627. * @see \Cake\ORM\Table::hasMany()
  628. * @see \Cake\ORM\Table::belongsToMany()
  629. */
  630. public function addAssociations(array $params)
  631. {
  632. foreach ($params as $assocType => $tables) {
  633. foreach ($tables as $associated => $options) {
  634. if (is_numeric($associated)) {
  635. $associated = $options;
  636. $options = [];
  637. }
  638. $this->{$assocType}($associated, $options);
  639. }
  640. }
  641. }
  642. /**
  643. * Creates a new BelongsTo association between this table and a target
  644. * table. A "belongs to" association is a N-1 relationship where this table
  645. * is the N side, and where there is a single associated record in the target
  646. * table for each one in this table.
  647. *
  648. * Target table can be inferred by its name, which is provided in the
  649. * first argument, or you can either pass the to be instantiated or
  650. * an instance of it directly.
  651. *
  652. * The options array accept the following keys:
  653. *
  654. * - className: The class name of the target table object
  655. * - targetTable: An instance of a table object to be used as the target table
  656. * - foreignKey: The name of the field to use as foreign key, if false none
  657. * will be used
  658. * - conditions: array with a list of conditions to filter the join with
  659. * - joinType: The type of join to be used (e.g. INNER)
  660. *
  661. * This method will return the association object that was built.
  662. *
  663. * @param string $associated the alias for the target table. This is used to
  664. * uniquely identify the association
  665. * @param array $options list of options to configure the association definition
  666. * @return \Cake\ORM\Association\BelongsTo
  667. */
  668. public function belongsTo($associated, array $options = [])
  669. {
  670. $options += ['sourceTable' => $this];
  671. $association = new BelongsTo($associated, $options);
  672. return $this->_associations->add($association->name(), $association);
  673. }
  674. /**
  675. * Creates a new HasOne association between this table and a target
  676. * table. A "has one" association is a 1-1 relationship.
  677. *
  678. * Target table can be inferred by its name, which is provided in the
  679. * first argument, or you can either pass the class name to be instantiated or
  680. * an instance of it directly.
  681. *
  682. * The options array accept the following keys:
  683. *
  684. * - className: The class name of the target table object
  685. * - targetTable: An instance of a table object to be used as the target table
  686. * - foreignKey: The name of the field to use as foreign key, if false none
  687. * will be used
  688. * - dependent: Set to true if you want CakePHP to cascade deletes to the
  689. * associated table when an entity is removed on this table. Set to false
  690. * if you don't want CakePHP to remove associated data, for when you are using
  691. * database constraints.
  692. * - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on
  693. * cascaded deletes. If false the ORM will use deleteAll() to remove data.
  694. * When true records will be loaded and then deleted.
  695. * - conditions: array with a list of conditions to filter the join with
  696. * - joinType: The type of join to be used (e.g. LEFT)
  697. *
  698. * This method will return the association object that was built.
  699. *
  700. * @param string $associated the alias for the target table. This is used to
  701. * uniquely identify the association
  702. * @param array $options list of options to configure the association definition
  703. * @return \Cake\ORM\Association\HasOne
  704. */
  705. public function hasOne($associated, array $options = [])
  706. {
  707. $options += ['sourceTable' => $this];
  708. $association = new HasOne($associated, $options);
  709. return $this->_associations->add($association->name(), $association);
  710. }
  711. /**
  712. * Creates a new HasMany association between this table and a target
  713. * table. A "has many" association is a 1-N relationship.
  714. *
  715. * Target table can be inferred by its name, which is provided in the
  716. * first argument, or you can either pass the class name to be instantiated or
  717. * an instance of it directly.
  718. *
  719. * The options array accept the following keys:
  720. *
  721. * - className: The class name of the target table object
  722. * - targetTable: An instance of a table object to be used as the target table
  723. * - foreignKey: The name of the field to use as foreign key, if false none
  724. * will be used
  725. * - dependent: Set to true if you want CakePHP to cascade deletes to the
  726. * associated table when an entity is removed on this table. Set to false
  727. * if you don't want CakePHP to remove associated data, for when you are using
  728. * database constraints.
  729. * - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on
  730. * cascaded deletes. If false the ORM will use deleteAll() to remove data.
  731. * When true records will be loaded and then deleted.
  732. * - conditions: array with a list of conditions to filter the join with
  733. * - sort: The order in which results for this association should be returned
  734. * - strategy: The strategy to be used for selecting results Either 'select'
  735. * or 'subquery'. If subquery is selected the query used to return results
  736. * in the source table will be used as conditions for getting rows in the
  737. * target table.
  738. *
  739. * This method will return the association object that was built.
  740. *
  741. * @param string $associated the alias for the target table. This is used to
  742. * uniquely identify the association
  743. * @param array $options list of options to configure the association definition
  744. * @return \Cake\ORM\Association\HasMany
  745. */
  746. public function hasMany($associated, array $options = [])
  747. {
  748. $options += ['sourceTable' => $this];
  749. $association = new HasMany($associated, $options);
  750. return $this->_associations->add($association->name(), $association);
  751. }
  752. /**
  753. * Creates a new BelongsToMany association between this table and a target
  754. * table. A "belongs to many" association is a M-N relationship.
  755. *
  756. * Target table can be inferred by its name, which is provided in the
  757. * first argument, or you can either pass the class name to be instantiated or
  758. * an instance of it directly.
  759. *
  760. * The options array accept the following keys:
  761. *
  762. * - className: The class name of the target table object.
  763. * - targetTable: An instance of a table object to be used as the target table.
  764. * - foreignKey: The name of the field to use as foreign key.
  765. * - targetForeignKey: The name of the field to use as the target foreign key.
  766. * - joinTable: The name of the table representing the link between the two
  767. * - through: If you choose to use an already instantiated link table, set this
  768. * key to a configured Table instance containing associations to both the source
  769. * and target tables in this association.
  770. * - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on
  771. * cascaded deletes. If false the ORM will use deleteAll() to remove data.
  772. * When true join/junction table records will be loaded and then deleted.
  773. * - conditions: array with a list of conditions to filter the join with.
  774. * - sort: The order in which results for this association should be returned.
  775. * - strategy: The strategy to be used for selecting results Either 'select'
  776. * or 'subquery'. If subquery is selected the query used to return results
  777. * in the source table will be used as conditions for getting rows in the
  778. * target table.
  779. * - saveStrategy: Either 'append' or 'replace'. Indicates the mode to be used
  780. * for saving associated entities. The former will only create new links
  781. * between both side of the relation and the latter will do a wipe and
  782. * replace to create the links between the passed entities when saving.
  783. *
  784. * This method will return the association object that was built.
  785. *
  786. * @param string $associated the alias for the target table. This is used to
  787. * uniquely identify the association
  788. * @param array $options list of options to configure the association definition
  789. * @return \Cake\ORM\Association\BelongsToMany
  790. */
  791. public function belongsToMany($associated, array $options = [])
  792. {
  793. $options += ['sourceTable' => $this];
  794. $association = new BelongsToMany($associated, $options);
  795. return $this->_associations->add($association->name(), $association);
  796. }
  797. /**
  798. * {@inheritDoc}
  799. *
  800. * By default, `$options` will recognize the following keys:
  801. *
  802. * - fields
  803. * - conditions
  804. * - order
  805. * - limit
  806. * - offset
  807. * - page
  808. * - order
  809. * - group
  810. * - having
  811. * - contain
  812. * - join
  813. * @return \Cake\ORM\Query
  814. */
  815. public function find($type = 'all', $options = [])
  816. {
  817. $query = $this->query();
  818. $query->select();
  819. return $this->callFinder($type, $query, $options);
  820. }
  821. /**
  822. * Returns the query as passed
  823. *
  824. * @param \Cake\ORM\Query $query The query to find with
  825. * @param array $options The options to use for the find
  826. * @return \Cake\ORM\Query
  827. */
  828. public function findAll(Query $query, array $options)
  829. {
  830. return $query;
  831. }
  832. /**
  833. * Sets up a query object so results appear as an indexed array, useful for any
  834. * place where you would want a list such as for populating input select boxes.
  835. *
  836. * When calling this finder, the fields passed are used to determine what should
  837. * be used as the array key, value and optionally what to group the results by.
  838. * By default the primary key for the model is used for the key, and the display
  839. * field as value.
  840. *
  841. * The results of this finder will be in the following form:
  842. *
  843. * ```
  844. * [
  845. * 1 => 'value for id 1',
  846. * 2 => 'value for id 2',
  847. * 4 => 'value for id 4'
  848. * ]
  849. * ```
  850. *
  851. * You can specify which property will be used as the key and which as value
  852. * by using the `$options` array, when not specified, it will use the results
  853. * of calling `primaryKey` and `displayField` respectively in this table:
  854. *
  855. * ```
  856. * $table->find('list', [
  857. * 'keyField' => 'name',
  858. * 'valueField' => 'age'
  859. * ]);
  860. * ```
  861. *
  862. * Results can be put together in bigger groups when they share a property, you
  863. * can customize the property to use for grouping by setting `groupField`:
  864. *
  865. * ```
  866. * $table->find('list', [
  867. * 'groupField' => 'category_id',
  868. * ]);
  869. * ```
  870. *
  871. * When using a `groupField` results will be returned in this format:
  872. *
  873. * ```
  874. * [
  875. * 'group_1' => [
  876. * 1 => 'value for id 1',
  877. * 2 => 'value for id 2',
  878. * ]
  879. * 'group_2' => [
  880. * 4 => 'value for id 4'
  881. * ]
  882. * ]
  883. * ```
  884. *
  885. * @param \Cake\ORM\Query $query The query to find with
  886. * @param array $options The options for the find
  887. * @return \Cake\ORM\Query
  888. */
  889. public function findList(Query $query, array $options)
  890. {
  891. $options += [
  892. 'keyField' => $this->primaryKey(),
  893. 'valueField' => $this->displayField(),
  894. 'groupField' => null
  895. ];
  896. if (isset($options['idField'])) {
  897. $options['keyField'] = $options['idField'];
  898. unset($options['idField']);
  899. trigger_error('Option "idField" is deprecated, use "keyField" instead.', E_USER_WARNING);
  900. }
  901. $options = $this->_setFieldMatchers(
  902. $options,
  903. ['keyField', 'valueField', 'groupField']
  904. );
  905. return $query->formatResults(function ($results) use ($options) {
  906. return $results->combine(
  907. $options['keyField'],
  908. $options['valueField'],
  909. $options['groupField']
  910. );
  911. });
  912. }
  913. /**
  914. * Results for this finder will be a nested array, and is appropriate if you want
  915. * to use the parent_id field of your model data to build nested results.
  916. *
  917. * Values belonging to a parent row based on their parent_id value will be
  918. * recursively nested inside the parent row values using the `children` property
  919. *
  920. * You can customize what fields are used for nesting results, by default the
  921. * primary key and the `parent_id` fields are used. If you wish to change
  922. * these defaults you need to provide the keys `keyField` or `parentField` in
  923. * `$options`:
  924. *
  925. * ```
  926. * $table->find('threaded', [
  927. * 'keyField' => 'id',
  928. * 'parentField' => 'ancestor_id'
  929. * ]);
  930. * ```
  931. *
  932. * @param \Cake\ORM\Query $query The query to find with
  933. * @param array $options The options to find with
  934. * @return \Cake\ORM\Query
  935. */
  936. public function findThreaded(Query $query, array $options)
  937. {
  938. $options += [
  939. 'keyField' => $this->primaryKey(),
  940. 'parentField' => 'parent_id',
  941. ];
  942. if (isset($options['idField'])) {
  943. $options['keyField'] = $options['idField'];
  944. unset($options['idField']);
  945. trigger_error('Option "idField" is deprecated, use "keyField" instead.', E_USER_WARNING);
  946. }
  947. $options = $this->_setFieldMatchers($options, ['keyField', 'parentField']);
  948. return $query->formatResults(function ($results) use ($options) {
  949. return $results->nest($options['keyField'], $options['parentField']);
  950. });
  951. }
  952. /**
  953. * Out of an options array, check if the keys described in `$keys` are arrays
  954. * and change the values for closures that will concatenate the each of the
  955. * properties in the value array when passed a row.
  956. *
  957. * This is an auxiliary function used for result formatters that can accept
  958. * composite keys when comparing values.
  959. *
  960. * @param array $options the original options passed to a finder
  961. * @param array $keys the keys to check in $options to build matchers from
  962. * the associated value
  963. * @return array
  964. */
  965. protected function _setFieldMatchers($options, $keys)
  966. {
  967. foreach ($keys as $field) {
  968. if (!is_array($options[$field])) {
  969. continue;
  970. }
  971. if (count($options[$field]) === 1) {
  972. $options[$field] = current($options[$field]);
  973. continue;
  974. }
  975. $fields = $options[$field];
  976. $options[$field] = function ($row) use ($fields) {
  977. $matches = [];
  978. foreach ($fields as $field) {
  979. $matches[] = $row[$field];
  980. }
  981. return implode(';', $matches);
  982. };
  983. }
  984. return $options;
  985. }
  986. /**
  987. * {@inheritDoc}
  988. *
  989. * @throws Cake\Datasource\Exception\InvalidPrimaryKeyException When $primaryKey has an
  990. * incorrect number of elements.
  991. */
  992. public function get($primaryKey, $options = [])
  993. {
  994. $key = (array)$this->primaryKey();
  995. $alias = $this->alias();
  996. foreach ($key as $index => $keyname) {
  997. $key[$index] = $alias . '.' . $keyname;
  998. }
  999. $primaryKey = (array)$primaryKey;
  1000. if (count($key) !== count($primaryKey)) {
  1001. $primaryKey = $primaryKey ?: [null];
  1002. $primaryKey = array_map(function ($key) {
  1003. return var_export($key, true);
  1004. }, $primaryKey);
  1005. throw new InvalidPrimaryKeyException(sprintf(
  1006. 'Record not found in table "%s" with primary key [%s]',
  1007. $this->table(),
  1008. implode($primaryKey, ', ')
  1009. ));
  1010. }
  1011. $conditions = array_combine($key, $primaryKey);
  1012. $cacheConfig = isset($options['cache']) ? $options['cache'] : false;
  1013. $cacheKey = isset($options['key']) ? $options['key'] : false;
  1014. unset($options['key'], $options['cache']);
  1015. $query = $this->find('all', $options)->where($conditions);
  1016. if ($cacheConfig) {
  1017. if (!$cacheKey) {
  1018. $cacheKey = sprintf(
  1019. "get:%s.%s%s",
  1020. $this->connection()->configName(),
  1021. $this->table(),
  1022. json_encode($primaryKey)
  1023. );
  1024. }
  1025. $query->cache($cacheKey, $cacheConfig);
  1026. }
  1027. return $query->firstOrFail();
  1028. }
  1029. /**
  1030. * Finds an existing record or creates a new one.
  1031. *
  1032. * Using the attributes defined in $search a find() will be done to locate
  1033. * an existing record. If that record exists it will be returned. If it does
  1034. * not exist, a new entity will be created with the $search properties, and
  1035. * the $defaults. When a new entity is created, it will be saved.
  1036. *
  1037. * @param array $search The criteria to find existing records by.
  1038. * @param callable|null $callback A callback that will be invoked for newly
  1039. * created entities. This callback will be called *before* the entity
  1040. * is persisted.
  1041. * @return \Cake\Datasource\EntityInterface An entity.
  1042. */
  1043. public function findOrCreate($search, callable $callback = null)
  1044. {
  1045. $query = $this->find()->where($search);
  1046. $row = $query->first();
  1047. if ($row) {
  1048. return $row;
  1049. }
  1050. $entity = $this->newEntity();
  1051. $entity->set($search, ['guard' => false]);
  1052. if ($callback) {
  1053. $callback($entity);
  1054. }
  1055. return $this->save($entity) ?: $entity;
  1056. }
  1057. /**
  1058. * {@inheritDoc}
  1059. */
  1060. public function query()
  1061. {
  1062. return new Query($this->connection(), $this);
  1063. }
  1064. /**
  1065. * {@inheritDoc}
  1066. */
  1067. public function updateAll($fields, $conditions)
  1068. {
  1069. $query = $this->query();
  1070. $query->update()
  1071. ->set($fields)
  1072. ->where($conditions);
  1073. $statement = $query->execute();
  1074. $statement->closeCursor();
  1075. return $statement->rowCount();
  1076. }
  1077. /**
  1078. * Returns the validation rules tagged with $name. It is possible to have
  1079. * multiple different named validation sets, this is useful when you need
  1080. * to use varying rules when saving from different routines in your system.
  1081. *
  1082. * There are two different ways of creating and naming validation sets: by
  1083. * creating a new method inside your own Table subclass, or by building
  1084. * the validator object yourself and storing it using this method.
  1085. *
  1086. * For example, if you wish to create a validation set called 'forSubscription',
  1087. * you will need to create a method in your Table subclass as follows:
  1088. *
  1089. * ```
  1090. * public function validationForSubscription($validator)
  1091. * {
  1092. * return $validator
  1093. * ->add('email', 'valid-email', ['rule' => 'email'])
  1094. * ->add('password', 'valid', ['rule' => 'notEmpty'])
  1095. * ->requirePresence('username');
  1096. * }
  1097. * ```
  1098. *
  1099. * Otherwise, you can build the object by yourself and store it in the Table object:
  1100. *
  1101. * ```
  1102. * $validator = new \Cake\Validation\Validator($table);
  1103. * $validator
  1104. * ->add('email', 'valid-email', ['rule' => 'email'])
  1105. * ->add('password', 'valid', ['rule' => 'notEmpty'])
  1106. * ->allowEmpty('bio');
  1107. * $table->validator('forSubscription', $validator);
  1108. * ```
  1109. *
  1110. * You can implement the method in `validationDefault` in your Table subclass
  1111. * should you wish to have a validation set that applies in cases where no other
  1112. * set is specified.
  1113. *
  1114. * @param string $name the name of the validation set to return
  1115. * @param \Cake\Validation\Validator|null $validator The validator instance to store,
  1116. * use null to get a validator.
  1117. * @return \Cake\Validation\Validator
  1118. */
  1119. public function validator($name = self::DEFAULT_VALIDATOR, Validator $validator = null)
  1120. {
  1121. if ($validator === null && isset($this->_validators[$name])) {
  1122. return $this->_validators[$name];
  1123. }
  1124. if ($validator === null) {
  1125. $validator = new Validator();
  1126. $validator = $this->{'validation' . ucfirst($name)}($validator);
  1127. $this->dispatchEvent('Model.buildValidator', compact('validator', 'name'));
  1128. }
  1129. $validator->provider('table', $this);
  1130. return $this->_validators[$name] = $validator;
  1131. }
  1132. /**
  1133. * Returns the default validator object. Subclasses can override this function
  1134. * to add a default validation set to the validator object.
  1135. *
  1136. * @param \Cake\Validation\Validator $validator The validator that can be modified to
  1137. * add some rules to it.
  1138. * @return \Cake\Validation\Validator
  1139. */
  1140. public function validationDefault(Validator $validator)
  1141. {
  1142. return $validator;
  1143. }
  1144. /**
  1145. * {@inheritDoc}
  1146. */
  1147. public function deleteAll($conditions)
  1148. {
  1149. $query = $this->query()
  1150. ->delete()
  1151. ->where($conditions);
  1152. $statement = $query->execute();
  1153. $statement->closeCursor();
  1154. return $statement->rowCount();
  1155. }
  1156. /**
  1157. * {@inheritDoc}
  1158. */
  1159. public function exists($conditions)
  1160. {
  1161. return (bool)count(
  1162. $this->find('all')
  1163. ->select(['existing' => 1])
  1164. ->where($conditions)
  1165. ->limit(1)
  1166. ->hydrate(false)
  1167. ->toArray()
  1168. );
  1169. }
  1170. /**
  1171. * {@inheritDoc}
  1172. *
  1173. * ### Options
  1174. *
  1175. * The options array can receive the following keys:
  1176. *
  1177. * - atomic: Whether to execute the save and callbacks inside a database
  1178. * transaction (default: true)
  1179. * - checkRules: Whether or not to check the rules on entity before saving, if the checking
  1180. * fails, it will abort the save operation. (default:true)
  1181. * - associated: If true it will save all associated entities as they are found
  1182. * in the passed `$entity` whenever the property defined for the association
  1183. * is marked as dirty. Associated records are saved recursively unless told
  1184. * otherwise. If an array, it will be interpreted as the list of associations
  1185. * to be saved. It is possible to provide different options for saving on associated
  1186. * table objects using this key by making the custom options the array value.
  1187. * If false no associated records will be saved. (default: true)
  1188. * - checkExisting: Whether or not to check if the entity already exists, assuming that the
  1189. * entity is marked as not new, and the primary key has been set.
  1190. *
  1191. * ### Events
  1192. *
  1193. * When saving, this method will trigger four events:
  1194. *
  1195. * - Model.beforeRules: Will be triggered right before any rule checking is done
  1196. * for the passed entity if the `checkRules` key in $options is not set to false.
  1197. * Listeners will receive as arguments the entity, options array and the operation type.
  1198. * If the event is stopped the checking result will be set to the result of the event itself.
  1199. * - Model.afterRules: Will be triggered right after the `checkRules()` method is
  1200. * called for the entity. Listeners will receive as arguments the entity,
  1201. * options array, the result of checking the rules and the operation type.
  1202. * If the event is stopped the checking result will be set to the result of
  1203. * the event itself.
  1204. * - Model.beforeSave: Will be triggered just before the list of fields to be
  1205. * persisted is calculated. It receives both the entity and the options as
  1206. * arguments. The options array is passed as an ArrayObject, so any changes in
  1207. * it will be reflected in every listener and remembered at the end of the event
  1208. * so it can be used for the rest of the save operation. Returning false in any
  1209. * of the listeners will abort the saving process. If the event is stopped
  1210. * using the event API, the event object's `result` property will be returned.
  1211. * This can be useful when having your own saving strategy implemented inside a
  1212. * listener.
  1213. * - Model.afterSave: Will be triggered after a successful insert or save,
  1214. * listeners will receive the entity and the options array as arguments. The type
  1215. * of operation performed (insert or update) can be determined by checking the
  1216. * entity's method `isNew`, true meaning an insert and false an update.
  1217. *
  1218. * This method will determine whether the passed entity needs to be
  1219. * inserted or updated in the database. It does that by checking the `isNew`
  1220. * method on the entity. If the entity to be saved returns a non-empty value from
  1221. * its `errors()` method, it will not be saved.
  1222. *
  1223. * ### Saving on associated tables
  1224. *
  1225. * This method will by default persist entities belonging to associated tables,
  1226. * whenever a dirty property matching the name of the property name set for an
  1227. * association in this table. It is possible to control what associations will
  1228. * be saved and to pass additional option for saving them.
  1229. *
  1230. * ```
  1231. * // Only save the comments association
  1232. * $articles->save($entity, ['associated' => ['Comments']);
  1233. *
  1234. * // Save the company, the employees and related addresses for each of them.
  1235. * // For employees do not check the entity rules
  1236. * $companies->save($entity, [
  1237. * 'associated' => [
  1238. * 'Employees' => [
  1239. * 'associated' => ['Addresses'],
  1240. * 'checkRules' => false
  1241. * ]
  1242. * ]
  1243. * ]);
  1244. *
  1245. * // Save no associations
  1246. * $articles->save($entity, ['associated' => false]);
  1247. * ```
  1248. *
  1249. */
  1250. public function save(EntityInterface $entity, $options = [])
  1251. {
  1252. $options = new ArrayObject($options + [
  1253. 'atomic' => true,
  1254. 'associated' => true,
  1255. 'checkRules' => true,
  1256. 'checkExisting' => true
  1257. ]);
  1258. if ($entity->errors()) {
  1259. return false;
  1260. }
  1261. if ($entity->isNew() === false && !$entity->dirty()) {
  1262. return $entity;
  1263. }
  1264. if ($options['atomic']) {
  1265. $connection = $this->connection();
  1266. $success = $connection->transactional(function () use ($entity, $options) {
  1267. return $this->_processSave($entity, $options);
  1268. });
  1269. if ($success) {
  1270. $this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
  1271. $entity->isNew(false);
  1272. $entity->source($this->registryAlias());
  1273. }
  1274. } else {
  1275. $success = $this->_processSave($entity, $options);
  1276. }
  1277. return $success;
  1278. }
  1279. /**
  1280. * Performs the actual saving of an entity based on the passed options.
  1281. *
  1282. * @param \Cake\Datasource\EntityInterface $entity the entity to be saved
  1283. * @param \ArrayObject $options the options to use for the save operation
  1284. * @return \Cake\Datasource\EntityInterface|bool
  1285. * @throws \RuntimeException When an entity is missing some of the primary keys.
  1286. */
  1287. protected function _processSave($entity, $options)
  1288. {
  1289. $primaryColumns = (array)$this->primaryKey();
  1290. if ($options['checkExisting'] && $primaryColumns && $entity->isNew() && $entity->has($primaryColumns)) {
  1291. $alias = $this->alias();
  1292. $conditions = [];
  1293. foreach ($entity->extract($primaryColumns) as $k => $v) {
  1294. $conditions["$alias.$k"] = $v;
  1295. }
  1296. $entity->isNew(!$this->exists($conditions));
  1297. }
  1298. $mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
  1299. if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) {
  1300. return false;
  1301. }
  1302. $options['associated'] = $this->_associations->normalizeKeys($options['associated']);
  1303. $event = $this->dispatchEvent('Model.beforeSave', compact('entity', 'options'));
  1304. if ($event->isStopped()) {
  1305. return $event->result;
  1306. }
  1307. $saved = $this->_associations->saveParents(
  1308. $this,
  1309. $entity,
  1310. $options['associated'],
  1311. $options->getArrayCopy()
  1312. );
  1313. if (!$saved && $options['atomic']) {
  1314. return false;
  1315. }
  1316. $data = $entity->extract($this->schema()->columns(), true);
  1317. $isNew = $entity->isNew();
  1318. if ($isNew) {
  1319. $success = $this->_insert($entity, $data);
  1320. } else {
  1321. $success = $this->_update($entity, $data);
  1322. }
  1323. if ($success) {
  1324. $success = $this->_associations->saveChildren(
  1325. $this,
  1326. $entity,
  1327. $options['associated'],
  1328. $options->getArrayCopy()
  1329. );
  1330. if ($success || !$options['atomic']) {
  1331. $entity->clean();
  1332. $this->dispatchEvent('Model.afterSave', compact('entity', 'options'));
  1333. if (!$options['atomic']) {
  1334. $entity->isNew(false);
  1335. $entity->source($this->registryAlias());
  1336. }
  1337. $success = true;
  1338. }
  1339. }
  1340. if (!$success && $isNew) {
  1341. $entity->unsetProperty($this->primaryKey());
  1342. $entity->isNew(true);
  1343. }
  1344. if ($success) {
  1345. return $entity;
  1346. }
  1347. return false;
  1348. }
  1349. /**
  1350. * Auxiliary function to handle the insert of an entity's data in the table
  1351. *
  1352. * @param \Cake\Datasource\EntityInterface $entity the subject entity from were $data was extracted
  1353. * @param array $data The actual data that needs to be saved
  1354. * @return \Cake\Datasource\EntityInterface|bool
  1355. * @throws \RuntimeException if not all the primary keys where supplied or could
  1356. * be generated when the table has composite primary keys. Or when the table has no primary key.
  1357. */
  1358. protected function _insert($entity, $data)
  1359. {
  1360. $primary = (array)$this->primaryKey();
  1361. if (empty($primary)) {
  1362. $msg = sprintf(
  1363. 'Cannot insert row in "%s" table, it has no primary key.',
  1364. $this->table()
  1365. );
  1366. throw new \RuntimeException($msg);
  1367. }
  1368. $keys = array_fill(0, count($primary), null);
  1369. $id = (array)$this->_newId($primary) + $keys;
  1370. $primary = array_combine($primary, $id);
  1371. $filteredKeys = array_filter($primary, 'strlen');
  1372. $data = $filteredKeys + $data;
  1373. if (count($primary) > 1) {
  1374. foreach ($primary as $k => $v) {
  1375. if (!isset($data[$k])) {
  1376. $msg = 'Cannot insert row, some of the primary key values are missing. ';
  1377. $msg .= sprintf(
  1378. 'Got (%s), expecting (%s)',
  1379. implode(', ', $filteredKeys + $entity->extract(array_keys($primary))),
  1380. implode(', ', array_keys($primary))
  1381. );
  1382. throw new \RuntimeException($msg);
  1383. }
  1384. }
  1385. }
  1386. $success = false;
  1387. if (empty($data)) {
  1388. return $success;
  1389. }
  1390. $statement = $this->query()->insert(array_keys($data))
  1391. ->values($data)
  1392. ->execute();
  1393. if ($statement->rowCount() !== 0) {
  1394. $success = $entity;
  1395. $entity->set($filteredKeys, ['guard' => false]);
  1396. foreach ($primary as $key => $v) {
  1397. if (!isset($data[$key])) {
  1398. $id = $statement->lastInsertId($this->table(), $key);
  1399. $entity->set($key, $id);
  1400. break;
  1401. }
  1402. }
  1403. }
  1404. $statement->closeCursor();
  1405. return $success;
  1406. }
  1407. /**
  1408. * Generate a primary key value for a new record.
  1409. *
  1410. * By default, this uses the type system to generate a new primary key
  1411. * value if possible. You can override this method if you have specific requirements
  1412. * for id generation.
  1413. *
  1414. * @param array $primary The primary key columns to get a new ID for.
  1415. * @return mixed Either null or the new primary key value.
  1416. */
  1417. protected function _newId($primary)
  1418. {
  1419. if (!$primary || count((array)$primary) > 1) {
  1420. return null;
  1421. }
  1422. $typeName = $this->schema()->columnType($primary[0]);
  1423. $type = Type::build($typeName);
  1424. return $type->newId();
  1425. }
  1426. /**
  1427. * Auxiliary function to handle the update of an entity's data in the table
  1428. *
  1429. * @param \Cake\Datasource\EntityInterface $entity the subject entity from were $data was extracted
  1430. * @param array $data The actual data that needs to be saved
  1431. * @return \Cake\Datasource\EntityInterface|bool
  1432. * @throws \InvalidArgumentException When primary key data is missing.
  1433. */
  1434. protected function _update($entity, $data)
  1435. {
  1436. $primaryColumns = (array)$this->primaryKey();
  1437. $primaryKey = $entity->extract($primaryColumns);
  1438. $data = array_diff_key($data, $primaryKey);
  1439. if (empty($data)) {
  1440. return $entity;
  1441. }
  1442. if (!$entity->has($primaryColumns)) {
  1443. $message = 'All primary key value(s) are needed for updating';
  1444. throw new \InvalidArgumentException($message);
  1445. }
  1446. $query = $this->query();
  1447. $statement = $query->update()
  1448. ->set($data)
  1449. ->where($primaryKey)
  1450. ->execute();
  1451. $success = false;
  1452. if ($statement->errorCode() === '00000') {
  1453. $success = $entity;
  1454. }
  1455. $statement->closeCursor();
  1456. return $success;
  1457. }
  1458. /**
  1459. * {@inheritDoc}
  1460. *
  1461. * For HasMany and HasOne associations records will be removed based on
  1462. * the dependent option. Join table records in BelongsToMany associations
  1463. * will always be removed. You can use the `cascadeCallbacks` option
  1464. * when defining associations to change how associated data is deleted.
  1465. *
  1466. * ### Options
  1467. *
  1468. * - `atomic` Defaults to true. When true the deletion happens within a transaction.
  1469. * - `checkRules` Defaults to true. Check deletion rules before deleting the record.
  1470. *
  1471. * ### Events
  1472. *
  1473. * - `beforeDelete` Fired before the delete occurs. If stopped the delete
  1474. * will be aborted. Receives the event, entity, and options.
  1475. * - `afterDelete` Fired after the delete has been successful. Receives
  1476. * the event, entity, and options.
  1477. *
  1478. * The options argument will be converted into an \ArrayObject instance
  1479. * for the duration of the callbacks, this allows listeners to modify
  1480. * the options used in the delete operation.
  1481. *
  1482. */
  1483. public function delete(EntityInterface $entity, $options = [])
  1484. {
  1485. $options = new ArrayObject($options + ['atomic' => true, 'checkRules' => true]);
  1486. $process = function () use ($entity, $options) {
  1487. return $this->_processDelete($entity, $options);
  1488. };
  1489. if ($options['atomic']) {
  1490. return $this->connection()->transactional($process);
  1491. }
  1492. return $process();
  1493. }
  1494. /**
  1495. * Perform the delete operation.
  1496. *
  1497. * Will delete the entity provided. Will remove rows from any
  1498. * dependent associations, and clear out join tables for BelongsToMany associations.
  1499. *
  1500. * @param \Cake\DataSource\EntityInterface $entity The entity to delete.
  1501. * @param \ArrayObject $options The options for the delete.
  1502. * @throws \InvalidArgumentException if there are no primary key values of the
  1503. * passed entity
  1504. * @return bool success
  1505. */
  1506. protected function _processDelete($entity, $options)
  1507. {
  1508. if ($entity->isNew()) {
  1509. return false;
  1510. }
  1511. $primaryKey = (array)$this->primaryKey();
  1512. if (!$entity->has($primaryKey)) {
  1513. $msg = 'Deleting requires all primary key values.';
  1514. throw new \InvalidArgumentException($msg);
  1515. }
  1516. if ($options['checkRules'] && !$this->checkRules($entity, RulesChecker::DELETE, $options)) {
  1517. return false;
  1518. }
  1519. $event = $this->dispatchEvent('Model.beforeDelete', [
  1520. 'entity' => $entity,
  1521. 'options' => $options
  1522. ]);
  1523. if ($event->isStopped()) {
  1524. return $event->result;
  1525. }
  1526. $this->_associations->cascadeDelete($entity, $options->getArrayCopy());
  1527. $query = $this->query();
  1528. $conditions = (array)$entity->extract($primaryKey);
  1529. $statement = $query->delete()
  1530. ->where($conditions)
  1531. ->execute();
  1532. $success = $statement->rowCount() > 0;
  1533. if (!$success) {
  1534. return $success;
  1535. }
  1536. $this->dispatchEvent('Model.afterDelete', [
  1537. 'entity' => $entity,
  1538. 'options' => $options
  1539. ]);
  1540. return $success;
  1541. }
  1542. /**
  1543. * Returns true if the finder exists for the table
  1544. *
  1545. * @param string $type name of finder to check
  1546. *
  1547. * @return bool
  1548. */
  1549. public function hasFinder($type)
  1550. {
  1551. $finder = 'find' . $type;
  1552. return method_exists($this, $finder) || ($this->_behaviors && $this->_behaviors->hasFinder($type));
  1553. }
  1554. /**
  1555. * Calls a finder method directly and applies it to the passed query,
  1556. * if no query is passed a new one will be created and returned
  1557. *
  1558. * @param string $type name of the finder to be called
  1559. * @param \Cake\ORM\Query $query The query object to apply the finder options to
  1560. * @param array $options List of options to pass to the finder
  1561. * @return \Cake\ORM\Query
  1562. * @throws \BadMethodCallException
  1563. */
  1564. public function callFinder($type, Query $query, array $options = [])
  1565. {
  1566. $query->applyOptions($options);
  1567. $options = $query->getOptions();
  1568. $finder = 'find' . $type;
  1569. if (method_exists($this, $finder)) {
  1570. return $this->{$finder}($query, $options);
  1571. }
  1572. if ($this->_behaviors && $this->_behaviors->hasFinder($type)) {
  1573. return $this->_behaviors->callFinder($type, [$query, $options]);
  1574. }
  1575. throw new \BadMethodCallException(
  1576. sprintf('Unknown finder method "%s"', $type)
  1577. );
  1578. }
  1579. /**
  1580. * Provides the dynamic findBy and findByAll methods.
  1581. *
  1582. * @param string $method The method name that was fired.
  1583. * @param array $args List of arguments passed to the function.
  1584. * @return mixed
  1585. * @throws \BadMethodCallException when there are missing arguments, or when
  1586. * and & or are combined.
  1587. */
  1588. protected function _dynamicFinder($method, $args)
  1589. {
  1590. $method = Inflector::underscore($method);
  1591. preg_match('/^find_([\w]+)_by_/', $method, $matches);
  1592. if (empty($matches)) {
  1593. // find_by_ is 8 characters.
  1594. $fields = substr($method, 8);
  1595. $findType = 'all';
  1596. } else {
  1597. $fields = substr($method, strlen($matches[0]));
  1598. $findType = Inflector::variable($matches[1]);
  1599. }
  1600. $hasOr = strpos($fields, '_or_');
  1601. $hasAnd = strpos($fields, '_and_');
  1602. $makeConditions = function ($fields, $args) {
  1603. $conditions = [];
  1604. if (count($args) < count($fields)) {
  1605. throw new BadMethodCallException(sprintf(
  1606. 'Not enough arguments for magic finder. Got %s required %s',
  1607. count($args),
  1608. count($fields)
  1609. ));
  1610. }
  1611. foreach ($fields as $field) {
  1612. $conditions[$field] = array_shift($args);
  1613. }
  1614. return $conditions;
  1615. };
  1616. if ($hasOr !== false && $hasAnd !== false) {
  1617. throw new BadMethodCallException(
  1618. 'Cannot mix "and" & "or" in a magic finder. Use find() instead.'
  1619. );
  1620. }
  1621. if ($hasOr === false && $hasAnd === false) {
  1622. $conditions = $makeConditions([$fields], $args);
  1623. } elseif ($hasOr !== false) {
  1624. $fields = explode('_or_', $fields);
  1625. $conditions = [
  1626. 'OR' => $makeConditions($fields, $args)
  1627. ];
  1628. } elseif ($hasAnd !== false) {
  1629. $fields = explode('_and_', $fields);
  1630. $conditions = $makeConditions($fields, $args);
  1631. }
  1632. return $this->find($findType, [
  1633. 'conditions' => $conditions,
  1634. ]);
  1635. }
  1636. /**
  1637. * Handles behavior delegation + dynamic finders.
  1638. *
  1639. * If your Table uses any behaviors you can call them as if
  1640. * they were on the table object.
  1641. *
  1642. * @param string $method name of the method to be invoked
  1643. * @param array $args List of arguments passed to the function
  1644. * @return mixed
  1645. * @throws \BadMethodCallException
  1646. */
  1647. public function __call($method, $args)
  1648. {
  1649. if ($this->_behaviors && $this->_behaviors->hasMethod($method)) {
  1650. return $this->_behaviors->call($method, $args);
  1651. }
  1652. if (preg_match('/^find(?:\w+)?By/', $method) > 0) {
  1653. return $this->_dynamicFinder($method, $args);
  1654. }
  1655. throw new \BadMethodCallException(
  1656. sprintf('Unknown method "%s"', $method)
  1657. );
  1658. }
  1659. /**
  1660. * Returns the association named after the passed value if exists, otherwise
  1661. * throws an exception.
  1662. *
  1663. * @param string $property the association name
  1664. * @return \Cake\ORM\Association
  1665. * @throws \RuntimeException if no association with such name exists
  1666. */
  1667. public function __get($property)
  1668. {
  1669. $association = $this->_associations->get($property);
  1670. if (!$association) {
  1671. throw new \RuntimeException(sprintf(
  1672. 'Table "%s" is not associated with "%s"',
  1673. get_class($this),
  1674. $property
  1675. ));
  1676. }
  1677. return $association;
  1678. }
  1679. /**
  1680. * Returns whether an association named after the passed value
  1681. * exists for this table.
  1682. *
  1683. * @param string $property the association name
  1684. * @return bool
  1685. */
  1686. public function __isset($property)
  1687. {
  1688. return $this->_associations->has($property);
  1689. }
  1690. /**
  1691. * Get the object used to marshal/convert array data into objects.
  1692. *
  1693. * Override this method if you want a table object to use custom
  1694. * marshalling logic.
  1695. *
  1696. * @return \Cake\ORM\Marshaller
  1697. * @see \Cake\ORM\Marshaller
  1698. */
  1699. public function marshaller()
  1700. {
  1701. return new Marshaller($this);
  1702. }
  1703. /**
  1704. * {@inheritDoc}
  1705. *
  1706. * By default all the associations on this table will be hydrated. You can
  1707. * limit which associations are built, or include deeper associations
  1708. * using the options parameter:
  1709. *
  1710. * ```
  1711. * $article = $this->Articles->newEntity(
  1712. * $this->request->data(),
  1713. * ['associated' => ['Tags', 'Comments.Users']]
  1714. * );
  1715. * ```
  1716. *
  1717. * You can limit fields that will be present in the constructed entity by
  1718. * passing the `fieldList` option, which is also accepted for associations:
  1719. *
  1720. * ```
  1721. * $article = $this->Articles->newEntity($this->request->data(), [
  1722. * 'fieldList' => ['title', 'body'],
  1723. * 'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']]
  1724. * ]
  1725. * );
  1726. * ```
  1727. *
  1728. * The `fieldList` option lets remove or restrict input data from ending up in
  1729. * the entity. If you'd like to relax the entity's default accessible fields,
  1730. * you can use the `accessibleFields` option:
  1731. *
  1732. * ```
  1733. * $article = $this->Articles->newEntity(
  1734. * $this->request->data(),
  1735. * ['accessibleFields' => ['protected_field' => true]]
  1736. * );
  1737. * ```
  1738. *
  1739. * By default, the data is validated before being passed to the new entity. In
  1740. * the case of invalid fields, those will not be present in the resulting object.
  1741. * The `validate` option can be used to disable validation on the passed data:
  1742. *
  1743. * ```
  1744. * $article = $this->Articles->newEntity(
  1745. * $this->request->data(),
  1746. * ['validate' => false]
  1747. * );
  1748. * ```
  1749. *
  1750. * You can also pass the name of the validator to use in the `validate` option.
  1751. * If `null` is passed to the first param of this function, no validation will
  1752. * be performed.
  1753. *
  1754. * You can use the `Model.beforeMarshal` event to modify request data
  1755. * before it is converted into entities.
  1756. */
  1757. public function newEntity($data = null, array $options = [])
  1758. {
  1759. if ($data === null) {
  1760. $class = $this->entityClass();
  1761. $entity = new $class([], ['source' => $this->registryAlias()]);
  1762. return $entity;
  1763. }
  1764. if (!isset($options['associated'])) {
  1765. $options['associated'] = $this->_associations->keys();
  1766. }
  1767. $marshaller = $this->marshaller();
  1768. return $marshaller->one($data, $options);
  1769. }
  1770. /**
  1771. * {@inheritDoc}
  1772. *
  1773. * By default all the associations on this table will be hydrated. You can
  1774. * limit which associations are built, or include deeper associations
  1775. * using the options parameter:
  1776. *
  1777. * ```
  1778. * $articles = $this->Articles->newEntities(
  1779. * $this->request->data(),
  1780. * ['associated' => ['Tags', 'Comments.Users']]
  1781. * );
  1782. * ```
  1783. *
  1784. * You can limit fields that will be present in the constructed entities by
  1785. * passing the `fieldList` option, which is also accepted for associations:
  1786. *
  1787. * ```
  1788. * $articles = $this->Articles->newEntities($this->request->data(), [
  1789. * 'fieldList' => ['title', 'body'],
  1790. * 'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']]
  1791. * ]
  1792. * );
  1793. * ```
  1794. *
  1795. * You can use the `Model.beforeMarshal` event to modify request data
  1796. * before it is converted into entities.
  1797. */
  1798. public function newEntities(array $data, array $options = [])
  1799. {
  1800. if (!isset($options['associated'])) {
  1801. $options['associated'] = $this->_associations->keys();
  1802. }
  1803. $marshaller = $this->marshaller();
  1804. return $marshaller->many($data, $options);
  1805. }
  1806. /**
  1807. * {@inheritDoc}
  1808. *
  1809. * When merging HasMany or BelongsToMany associations, all the entities in the
  1810. * `$data` array will appear, those that can be matched by primary key will get
  1811. * the data merged, but those that cannot, will be discarded.
  1812. *
  1813. * You can limit fields that will be present in the merged entity by
  1814. * passing the `fieldList` option, which is also accepted for associations:
  1815. *
  1816. * ```
  1817. * $article = $this->Articles->patchEntity($article, $this->request->data(), [
  1818. * 'fieldList' => ['title', 'body'],
  1819. * 'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']]
  1820. * ]
  1821. * );
  1822. * ```
  1823. *
  1824. * By default, the data is validated before being passed to the entity. In
  1825. * the case of invalid fields, those will not be assigned to the entity.
  1826. * The `validate` option can be used to disable validation on the passed data:
  1827. *
  1828. * ```
  1829. * $article = $this->patchEntity($article, $this->request->data(),[
  1830. * 'validate' => false
  1831. * ]);
  1832. * ```
  1833. *
  1834. * You can use the `Model.beforeMarshal` event to modify request data
  1835. * before it is converted into entities.
  1836. */
  1837. public function patchEntity(EntityInterface $entity, array $data, array $options = [])
  1838. {
  1839. if (!isset($options['associated'])) {
  1840. $options['associated'] = $this->_associations->keys();
  1841. }
  1842. $marshaller = $this->marshaller();
  1843. return $marshaller->merge($entity, $data, $options);
  1844. }
  1845. /**
  1846. * {@inheritDoc}
  1847. *
  1848. * Those entries in `$entities` that cannot be matched to any record in
  1849. * `$data` will be discarded. Records in `$data` that could not be matched will
  1850. * be marshalled as a new entity.
  1851. *
  1852. * When merging HasMany or BelongsToMany associations, all the entities in the
  1853. * `$data` array will appear, those that can be matched by primary key will get
  1854. * the data merged, but those that cannot, will be discarded.
  1855. *
  1856. * You can limit fields that will be present in the merged entities by
  1857. * passing the `fieldList` option, which is also accepted for associations:
  1858. *
  1859. * ```
  1860. * $articles = $this->Articles->patchEntities($articles, $this->request->data(), [
  1861. * 'fieldList' => ['title', 'body'],
  1862. * 'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']]
  1863. * ]
  1864. * );
  1865. * ```
  1866. *
  1867. * You can use the `Model.beforeMarshal` event to modify request data
  1868. * before it is converted into entities.
  1869. */
  1870. public function patchEntities($entities, array $data, array $options = [])
  1871. {
  1872. if (!isset($options['associated'])) {
  1873. $options['associated'] = $this->_associations->keys();
  1874. }
  1875. $marshaller = $this->marshaller();
  1876. return $marshaller->mergeMany($entities, $data, $options);
  1877. }
  1878. /**
  1879. * Validator method used to check the uniqueness of a value for a column.
  1880. * This is meant to be used with the validation API and not to be called
  1881. * directly.
  1882. *
  1883. * ### Example:
  1884. *
  1885. * ```
  1886. * $validator->add('email', [
  1887. * 'unique' => ['rule' => 'validateUnique', 'provider' => 'table']
  1888. * ])
  1889. * ```
  1890. *
  1891. * Unique validation can be scoped to the value of another column:
  1892. *
  1893. * ```
  1894. * $validator->add('email', [
  1895. * 'unique' => [
  1896. * 'rule' => ['validateUnique', ['scope' => 'site_id']],
  1897. * 'provider' => 'table'
  1898. * ]
  1899. * ]);
  1900. * ```
  1901. *
  1902. * In the above example, the email uniqueness will be scoped to only rows having
  1903. * the same site_id. Scoping will only be used if the scoping field is present in
  1904. * the data to be validated.
  1905. *
  1906. * @param mixed $value The value of column to be checked for uniqueness
  1907. * @param array $context Either the options or validation context.
  1908. * @param array|null $options The options array, optionally containing the 'scope' key
  1909. * @return bool true if the value is unique
  1910. */
  1911. public function validateUnique($value, array $context, array $options = null)
  1912. {
  1913. if ($options === null) {
  1914. $options = $context;
  1915. }
  1916. $entity = new Entity(
  1917. $options['data'],
  1918. [
  1919. 'useSetters' => false,
  1920. 'markNew' => $options['newRecord'],
  1921. 'source' => $this->registryAlias()
  1922. ]
  1923. );
  1924. $fields = array_merge(
  1925. [$options['field']],
  1926. isset($options['scope']) ? (array)$options['scope'] : []
  1927. );
  1928. $rule = new IsUnique($fields);
  1929. return $rule($entity, ['repository' => $this]);
  1930. }
  1931. /**
  1932. * Returns whether or not the passed entity complies with all the rules stored in
  1933. * the rules checker.
  1934. *
  1935. * @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
  1936. * @param string $operation The operation being run. Either 'create', 'update' or 'delete'.
  1937. * @param \ArrayObject|array $options The options To be passed to the rules.
  1938. * @return bool
  1939. */
  1940. public function checkRules(EntityInterface $entity, $operation = RulesChecker::CREATE, $options = null)
  1941. {
  1942. $rules = $this->rulesChecker();
  1943. $options = $options ?: new ArrayObject;
  1944. $options = is_array($options) ? new ArrayObject($options) : $options;
  1945. $event = $this->dispatchEvent(
  1946. 'Model.beforeRules',
  1947. compact('entity', 'options', 'operation')
  1948. );
  1949. if ($event->isStopped()) {
  1950. return $event->result;
  1951. }
  1952. $result = $rules->check($entity, $operation, $options->getArrayCopy());
  1953. $event = $this->dispatchEvent(
  1954. 'Model.afterRules',
  1955. compact('entity', 'options', 'result', 'operation')
  1956. );
  1957. if ($event->isStopped()) {
  1958. return $event->result;
  1959. }
  1960. return $result;
  1961. }
  1962. /**
  1963. * Returns the rule checker for this table. A rules checker object is used to
  1964. * test an entity for validity on rules that may involve complex logic or data that
  1965. * needs to be fetched from the database or other sources.
  1966. *
  1967. * @return \Cake\ORM\RulesChecker
  1968. */
  1969. public function rulesChecker()
  1970. {
  1971. if ($this->_rulesChecker !== null) {
  1972. return $this->_rulesChecker;
  1973. }
  1974. $this->_rulesChecker = $this->buildRules(new RulesChecker(['repository' => $this]));
  1975. $this->dispatchEvent('Model.buildRules', ['rules' => $this->_rulesChecker]);
  1976. return $this->_rulesChecker;
  1977. }
  1978. /**
  1979. * Returns rules checker object after modifying the one that was passed. Subclasses
  1980. * can override this method in order to initialize the rules to be applied to
  1981. * entities saved by this table.
  1982. *
  1983. * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
  1984. * @return \Cake\ORM\RulesChecker
  1985. */
  1986. public function buildRules(RulesChecker $rules)
  1987. {
  1988. return $rules;
  1989. }
  1990. /**
  1991. * Get the Model callbacks this table is interested in.
  1992. *
  1993. * By implementing the conventional methods a table class is assumed
  1994. * to be interested in the related event.
  1995. *
  1996. * Override this method if you need to add non-conventional event listeners.
  1997. * Or if you want you table to listen to non-standard events.
  1998. *
  1999. * @return array
  2000. */
  2001. public function implementedEvents()
  2002. {
  2003. $eventMap = [
  2004. 'Model.beforeMarshal' => 'beforeMarshal',
  2005. 'Model.beforeFind' => 'beforeFind',
  2006. 'Model.beforeSave' => 'beforeSave',
  2007. 'Model.afterSave' => 'afterSave',
  2008. 'Model.afterSaveCommit' => 'afterSaveCommit',
  2009. 'Model.beforeDelete' => 'beforeDelete',
  2010. 'Model.afterDelete' => 'afterDelete',
  2011. 'Model.beforeRules' => 'beforeRules',
  2012. 'Model.afterRules' => 'afterRules',
  2013. ];
  2014. $events = [];
  2015. foreach ($eventMap as $event => $method) {
  2016. if (!method_exists($this, $method)) {
  2017. continue;
  2018. }
  2019. $events[$event] = $method;
  2020. }
  2021. return $events;
  2022. }
  2023. /**
  2024. * Returns an array that can be used to describe the internal state of this
  2025. * object.
  2026. *
  2027. * @return array
  2028. */
  2029. public function __debugInfo()
  2030. {
  2031. $conn = $this->connection();
  2032. return [
  2033. 'registryAlias' => $this->registryAlias(),
  2034. 'table' => $this->table(),
  2035. 'alias' => $this->alias(),
  2036. 'entityClass' => $this->entityClass(),
  2037. 'associations' => $this->_associations->keys(),
  2038. 'behaviors' => $this->_behaviors->loaded(),
  2039. 'defaultConnection' => $this->defaultConnectionName(),
  2040. 'connectionName' => $conn ? $conn->configName() : null
  2041. ];
  2042. }
  2043. }