ソースを参照

Fix bitmasked to allow finder using contain.

mscherer 7 年 前
コミット
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.
 
+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`!
 
 ### Outview

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

@@ -58,6 +58,11 @@ class BitmaskedBehavior extends Behavior {
 		if (!isset($options['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']);
 

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

@@ -93,6 +93,18 @@ class BitmaskedBehaviorTest extends TestCase {
 	/**
 	 * @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() {
 		$data = [
 			'comment' => 'test save',