Browse Source

Fix bitmasked to allow finder using contain.

mscherer 7 years ago
parent
commit
db73185446

+ 5 - 0
docs/Behavior/Bitmasked.md

@@ -97,6 +97,11 @@ $searchManager
 
 
 This way the array of checkboxes selected will be turned into the integer bitmask needed for the query to work.
 This way the array of checkboxes selected will be turned into the integer bitmask needed for the query to work.
 
 
+When using select dropdows, you usually want to use type `contain` instead of `exact` matching:
+```php
+$this->Comments->find('bits', ['bits' => $statuses, 'type' => 'contain])->toArray();
+```
+
 Note: This requires Search `^4.2.1`!
 Note: This requires Search `^4.2.1`!
 
 
 ### Outview
 ### Outview

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

@@ -58,6 +58,11 @@ class BitmaskedBehavior extends Behavior {
 		if (!isset($options['bits'])) {
 		if (!isset($options['bits'])) {
 			throw new InvalidArgumentException("The 'bits' key is required for find('bits')");
 			throw new InvalidArgumentException("The 'bits' key is required for find('bits')");
 		}
 		}
+		$options += ['type' => 'exact'];
+
+		if ($options['type'] === 'contain') {
+			return $query->where($this->containsBit($options['bits']));
+		}
 
 
 		$bits = $this->encodeBitmask($options['bits']);
 		$bits = $this->encodeBitmask($options['bits']);
 
 

+ 12 - 0
tests/TestCase/Model/Behavior/BitmaskedBehaviorTest.php

@@ -93,6 +93,18 @@ class BitmaskedBehaviorTest extends TestCase {
 	/**
 	/**
 	 * @return void
 	 * @return void
 	 */
 	 */
+	public function testFindBitmaskedContain() {
+		$options = [
+			'bits' => [BitmaskedComment::STATUS_APPROVED],
+			'type' => 'contain'
+		];
+		$res = $this->Comments->find('bits', $options)->toArray();
+		$this->assertCount(3, $res);
+	}
+
+	/**
+	 * @return void
+	 */
 	public function testSaveBasic() {
 	public function testSaveBasic() {
 		$data = [
 		$data = [
 			'comment' => 'test save',
 			'comment' => 'test save',