Browse Source

Explain afterMarshal usage.

mscherer 5 years ago
parent
commit
e95f8a7bc7
2 changed files with 25 additions and 1 deletions
  1. 12 1
      docs/Behavior/Bitmasked.md
  2. 13 0
      src/Model/Behavior/BitmaskedBehavior.php

+ 12 - 1
docs/Behavior/Bitmasked.md

@@ -29,6 +29,9 @@ The `mappedField` param is quite handy if you want more control over your bitmas
 It stores the array under this alias and does not override the bitmask key.
 So in our case status will always contain the integer bitmask and statuses the verbose array of it.
 
+Note: If you use an alias, make sure that either also that alias is whitelisted for patching,
+or you don't use `beforeMarshal` event here.
+
 ### Defining the selectable values
 We first define values and make sure they follow the bitmask scheme:
 ```
@@ -58,7 +61,7 @@ public static function statuses($value = null) {
 
 Please note that you need to define Entity::enum() by extending my Tools Entity base class or by putting it into your own base class manually. You don’t have to use the enum approach, though.
 
-Of course it only makes sense to use bitmasks, if those values can co-exist, if you can select multiple at once. Otherwise you would want to store them separately anyway.
+Of course, it only makes sense to use bitmasks, if those values can co-exist, if you can select multiple at once. Otherwise you would want to store them separately anyway.
 Obviously you could also just use four or more boolean fields to achieve the same thing.
 
 So now, in the add/edit form we can:
@@ -111,6 +114,14 @@ $this->Comments->find('bits', ['bits' => $statuses, 'type' => 'contain])->toArra
 
 Note: This requires Search `^4.2.1`!
 
+### Configuration
+
+The default `onMarshal` expects you to require validation (not empty, ...) on this field.
+If you don't need that, and it is nullable, you can also set the event to e.g. `afterMarshal`.
+
+If you use `fields` config to whitelist the fields for patching, you should also whitelist
+the alias field if you defined one and if you are using `onMarshal`.
+
 ### Outview
 
 You can read more about how it began in [my blog post](https://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/).

+ 13 - 0
src/Model/Behavior/BitmaskedBehavior.php

@@ -171,6 +171,19 @@ class BitmaskedBehavior extends Behavior {
 	 * @param \Cake\Event\EventInterface $event
 	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
+	 * @return void
+	 */
+	public function afterMarshal(EventInterface $event, EntityInterface $entity, ArrayObject $options) {
+		if ($this->_config['on'] !== 'afterMarshal') {
+			return;
+		}
+		$this->encodeBitmaskData($entity);
+	}
+
+	/**
+	 * @param \Cake\Event\EventInterface $event
+	 * @param \Cake\Datasource\EntityInterface $entity
+	 * @param \ArrayObject $options
 	 * @param string $operation
 	 *
 	 * @return void