Skip to content

Commit

Permalink
Make entity context recognize schema defaults of associations.
Browse files Browse the repository at this point in the history
The entity passed to `_schemaDefault()` is not a valid value to use
with `_getTable()`, hence the root table has been used for all schema
lookups.
  • Loading branch information
ndm2 committed Jun 24, 2018
1 parent 9bd72b6 commit d710f9c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/View/Form/EntityContext.php
Expand Up @@ -249,7 +249,7 @@ public function val($field, $options = [])
}

if ($entity instanceof EntityInterface) {
$part = array_pop($parts);
$part = end($parts);
$val = $entity->get($part);
if ($val !== null) {
return $val;
Expand All @@ -261,7 +261,7 @@ public function val($field, $options = [])
return $options['default'];
}

return $this->_schemaDefault($part, $entity);
return $this->_schemaDefault($parts);
}
if (is_array($entity) || $entity instanceof ArrayAccess) {
$key = array_pop($parts);
Expand All @@ -275,16 +275,16 @@ public function val($field, $options = [])
/**
* Get default value from table schema for given entity field.
*
* @param string $field Field name.
* @param \Cake\Datasource\EntityInterface $entity The entity.
* @param array $parts Each one of the parts in a path for a field name
* @return mixed
*/
protected function _schemaDefault($field, $entity)
protected function _schemaDefault($parts)
{
$table = $this->_getTable($entity);
$table = $this->_getTable($parts);
if ($table === false) {
return null;
}
$field = end($parts);
$defaults = $table->getSchema()->defaultValues();
if (!array_key_exists($field, $defaults)) {
return null;
Expand Down
47 changes: 47 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -719,6 +719,53 @@ public function testValSchemaDefault()
$this->assertEquals('default title', $result);
}

/**
* Test getting association default value from table schema.
*
* @return void
*/
public function testValAssociatedSchemaDefault()
{
$table = $this->getTableLocator()->get('Articles');
$associatedTable = $table->hasMany('Comments')->getTarget();
$column = $associatedTable->getSchema()->getColumn('comment');
$associatedTable->getSchema()->addColumn('comment', ['default' => 'default comment'] + $column);
$row = $table->newEntity();

$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
$result = $context->val('comments.0.comment');
$this->assertEquals('default comment', $result);
}

/**
* Test getting association join table default value from table schema.
*
* @return void
*/
public function testValAssociatedJoinTableSchemaDefault()
{
$table = $this->getTableLocator()->get('Articles');
$joinTable = $table
->belongsToMany('Tags')
->setThrough('ArticlesTags')
->junction();
$joinTable->getSchema()->addColumn('column', [
'default' => 'default join table column value',
'type' => 'text'
]);
$row = $table->newEntity();

$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
$result = $context->val('tags.0._joinData.column');
$this->assertEquals('default join table column value', $result);
}

/**
* Test validator for boolean fields.
*
Expand Down

0 comments on commit d710f9c

Please sign in to comment.