Browse Source

fieldByConditions

Mark Scherer 11 years ago
parent
commit
385f5d0e26
2 changed files with 27 additions and 2 deletions
  1. 14 2
      src/Model/Table/Table.php
  2. 13 0
      tests/TestCase/Model/Table/TableTest.php

+ 14 - 2
src/Model/Table/Table.php

@@ -280,8 +280,8 @@ class Table extends CakeTable {
 	}
 
 	/**
-	 * Shim of 2.x field() method. Only difference: full $options array
-	 * instead of just conditions.
+	 * Convenience wrapper inspired by 2.x field() method. Only difference: full $options array
+	 * instead of just $conditions array.
 	 *
 	 * @param string $name
 	 * @param array $options
@@ -296,6 +296,18 @@ class Table extends CakeTable {
 	}
 
 	/**
+	 * Shim of 2.x field() method.
+	 *
+	 * @param string $name
+	 * @param array $conditions
+	 * @return mixed Field value or null if not available
+	 * @deprecated Port to field() with full $options array
+	 */
+	public function fieldByConditions($name, array $conditions = array()) {
+		return $this->field($name, ['conditions' => $conditions]);
+	}
+
+	/**
 	 * Sets the default ordering as 2.x shim.
 	 *
 	 * If you don't want that, don't call parent when overwriting it in extending classes.

+ 13 - 0
tests/TestCase/Model/Table/TableTest.php

@@ -126,6 +126,19 @@ class TableTest extends TestCase {
 	}
 
 	/**
+	 * TableTest::testField()
+	 *
+	 * @return void
+	 */
+	public function testFieldByConditions() {
+		$result = $this->Users->fieldByConditions('name', ['name' => 'User 1']);
+		$this->assertEquals('User 1', $result);
+
+		$result = $this->Users->fieldByConditions('name', ['name' => 'User xxx']);
+		$this->assertNull($result);
+	}
+
+	/**
 	 * Test 2.x shimmed order property
 	 *
 	 *   $this->order = array('field_name' => 'ASC') etc