Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add classname reflection to get table names when argument is omitted.

  • Loading branch information...
commit 2a87be0cd5a10ed437b1e199d214802e84442e4b 1 parent d90abb8
@markstory markstory authored
View
13 src/View/Form/EntityContext.php
@@ -17,6 +17,7 @@
use Cake\Network\Request;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
+use Cake\Utility\Inflector;
use Cake\Validation\Validator;
use Traversable;
@@ -92,9 +93,21 @@ public function __construct(Request $request, array $context) {
*/
protected function _prepare() {
$table = $this->_context['table'];
+ if (empty($table)) {
+ $entity = $this->_context['entity'];
+ if ($entity instanceof Entity) {
+ list($ns, $entityClass) = namespaceSplit(get_class($entity));
+ $table = Inflector::pluralize($entityClass);
+ }
+ }
if (is_string($table)) {
$table = TableRegistry::get($table);
}
+ if (!is_object($table)) {
+ throw new \RuntimeException(
+ 'Unable to find table class for current entity'
+ );
+ }
$alias = $this->_rootName = $table->alias();
$this->_tables[$alias] = $table;
}
View
29 tests/TestCase/View/Form/EntityContextTest.php
@@ -23,6 +23,12 @@
use Cake\View\Form\EntityContext;
/**
+ * Test stub.
+ */
+class Article extends Entity {
+}
+
+/**
* Entity context test case.
*/
class EntityContextTest extends TestCase {
@@ -45,6 +51,29 @@ public function setUp() {
}
/**
+ * Test operations that lack a table argument.
+ *
+ * @return void
+ */
+ public function testOperationsNoTableArg() {
+ $row = new Article([
+ 'title' => 'Test entity',
+ 'body' => 'Something new'
+ ]);
+ $row->errors('title', ['Title is required.']);
+
+ $context = new EntityContext($this->request, [
+ 'entity' => $row,
+ ]);
+
+ $result = $context->val('title');
+ $this->assertEquals($row->title, $result);
+
+ $result = $context->error('title');
+ $this->assertEquals($row->errors('title'), $result);
+ }
+
+/**
* Test reading data.
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.