ソースを参照

Fix Jsonable with find(list)

Mark Scherer 10 年 前
コミット
e3db75cf86

+ 5 - 1
src/Model/Behavior/JsonableBehavior.php

@@ -97,8 +97,12 @@ class JsonableBehavior extends Behavior {
 	 * @return void
 	 */
 	public function beforeFind(Event $event, Query $query) {
-		$query->formatResults(function (ResultSetInterface $results) {
+		$query->formatResults(function ($results) {
 			return $results->map(function ($row) {
+				if (!$row instanceOf Entity) {
+					return $row;
+				}
+
 				$this->decodeItems($row);
 				return $row;
 			});

+ 21 - 0
tests/TestCase/Model/Behavior/JsonableBehaviorTest.php

@@ -45,6 +45,27 @@ class JsonableBehaviorTest extends TestCase {
 	}
 
 	/**
+	 * Find list should still work
+	 *
+	 * @return void
+	 */
+	public function testList() {
+		$data = [
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'title' => 'some Name',
+			'details' => ['x' => 'y'],
+		];
+		$entity = $this->Comments->newEntity($data);
+		$res = $this->Comments->save($entity);
+		$this->assertTrue((bool)$res);
+
+		$result = $this->Comments->find('list');
+		$expected = [1 => 'some Name'];
+		$this->assertSame($expected, $result->toArray());
+	}
+
+	/**
 	 * JsonableBehaviorTest::testFieldsWithList()
 	 *
 	 * @return void