Browse Source

Merge pull request #4504 from cakephp/3.0-model-callbacks

Add beforeValidate and afterValidate callback methods to behaviors.
Mark Story 11 years ago
parent
commit
4d183f8453
3 changed files with 76 additions and 29 deletions
  1. 25 14
      src/ORM/Behavior.php
  2. 27 13
      src/ORM/Table.php
  3. 24 2
      tests/TestCase/ORM/BehaviorTest.php

+ 25 - 14
src/ORM/Behavior.php

@@ -46,24 +46,33 @@ use Cake\Event\EventListener;
  * CakePHP provides a number of lifecycle events your behaviors can
  * listen to:
  *
- * - `beforeFind(Event $event, Query $query)`
- *   Fired before a query is converted into SQL.
+ * - `beforeFind(Event $event, Query $query, ArrayObject $options, boolean $primary)`
+ *   Fired before each find operation. By stopping the event and supplying a
+ *   return value you can bypass the find operation entirely. Any changes done
+ *   to the $query instance will be retained for the rest of the find. The
+ *   $primary parameter indicates whether or not this is the root query,
+ *   or an associated query.
  *
- * - `beforeDelete(Event $event, Entity $entity)`
- *   Fired before an entity is deleted.
+ * - `beforeValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired before an entity is validated. By stopping this event, you can abort
+ *   the validate + save operations.
  *
- * - `afterDelete(Event $event, Entity $entity)`
- *   Fired after an entity has been deleted. The entity parameter
- *   will contain the entity state from before it was deleted.
+ * - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired after an entity is validated.
  *
- * - `beforeSave(Event $event, Entity $entity)`
- *   Fired before an entity is saved. In the case where
- *   multiple entities are being saved, one event will be fired
- *   for each entity.
+ * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired before each entity is saved. Stopping this event will abort the save
+ *   operation. When the event is stopped the result of the event will be returned.
  *
- * - `afterSave(Event $event, Entity $entity)`
- *   Fired after an entity is saved. The saved entity will be provided
- *   as a parameter.
+ * - `afterSave(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired after an entity is saved.
+ *
+ * - `beforeDelete(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired before an entity is deleted. By stopping this event you will abort
+ *   the delete operation.
+ *
+ * - `afterDelete(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired after an entity has been deleted.
  *
  * In addition to the core events, behaviors can respond to any
  * event fired from your Table classes including custom application
@@ -209,6 +218,8 @@ class Behavior implements EventListener {
 			'Model.afterSave' => 'afterSave',
 			'Model.beforeDelete' => 'beforeDelete',
 			'Model.afterDelete' => 'afterDelete',
+			'Model.beforeValidate' => 'beforeValidate',
+			'Model.afterValidate' => 'afterValidate',
 		];
 		$config = $this->config();
 		$priority = isset($config['priority']) ? $config['priority'] : null;

+ 27 - 13
src/ORM/Table.php

@@ -75,19 +75,33 @@ use Cake\Validation\Validator;
  * Table objects provide a few callbacks/events you can hook into to augment/replace
  * find operations. Each event uses the standard event subsystem in CakePHP
  *
- * - `beforeFind($event, $query, $options, $eagerLoaded)` - Fired before each find operation.
- *   By stopping the event and supplying a return value you can bypass the find operation
- *   entirely. Any changes done to the $query instance will be retained for the rest of the find.
- * - `beforeValidate($event, $entity, $options, $validator)` - Fired before an entity is validated.
- *   By stopping this event, you can abort the validate + save operations.
- * - `afterValidate($event, $entity, $options, $validator)` - Fired after an entity is validated.
- * - `beforeSave($event, $entity, $options)` - Fired before each entity is saved. Stopping this
- *   event will abort the save operation. When the event is stopped the result of the event will
- *   be returned.
- * - `afterSave($event, $entity, $options)` - Fired after an entity is saved.
- * - `beforeDelete($event, $entity, $options)` - Fired before an entity is deleted.
- *   By stopping this event you will abort the delete operation.
- * - `afterDelete($event, $entity, $options)` - Fired after an entity has been deleted.
+ * - `beforeFind(Event $event, Query $query, ArrayObject $options, boolean $primary)`
+ *   Fired before each find operation. By stopping the event and supplying a
+ *   return value you can bypass the find operation entirely. Any changes done
+ *   to the $query instance will be retained for the rest of the find. The
+ *   $primary parameter indicates whether or not this is the root query,
+ *   or an associated query.
+ *
+ * - `beforeValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired before an entity is validated. By stopping this event, you can abort
+ *   the validate + save operations.
+ *
+ * - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
+ *   Fired after an entity is validated.
+ *
+ * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired before each entity is saved. Stopping this event will abort the save
+ *   operation. When the event is stopped the result of the event will be returned.
+ *
+ * - `afterSave(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired after an entity is saved.
+ *
+ * - `beforeDelete(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired before an entity is deleted. By stopping this event you will abort
+ *   the delete operation.
+ *
+ * - `afterDelete(Event $event, Entity $entity, ArrayObject $options)`
+ *   Fired after an entity has been deleted.
  *
  * @see \Cake\Event\EventManager for reference on the events system.
  */

+ 24 - 2
tests/TestCase/ORM/BehaviorTest.php

@@ -28,6 +28,18 @@ class TestBehavior extends Behavior {
 	public function beforeFind() {
 	}
 
+/**
+ * Test for event bindings.
+ */
+	public function beforeValidate() {
+	}
+
+/**
+ * Test for event bindings.
+ */
+	public function afterValidate() {
+	}
+
 }
 
 /**
@@ -171,7 +183,9 @@ class BehaviorTest extends TestCase {
 		$table = $this->getMock('Cake\ORM\Table');
 		$behavior = new TestBehavior($table);
 		$expected = [
-			'Model.beforeFind' => 'beforeFind'
+			'Model.beforeFind' => 'beforeFind',
+			'Model.beforeValidate' => 'beforeValidate',
+			'Model.afterValidate' => 'afterValidate',
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());
 	}
@@ -188,7 +202,15 @@ class BehaviorTest extends TestCase {
 			'Model.beforeFind' => [
 				'priority' => 10,
 				'callable' => 'beforeFind'
-			]
+			],
+			'Model.beforeValidate' => [
+				'priority' => 10,
+				'callable' => 'beforeValidate'
+			],
+			'Model.afterValidate' => [
+				'priority' => 10,
+				'callable' => 'afterValidate'
+			],
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());
 	}