FIX: castingHelper failed to find many_many_extraFields data (fixes #4661)

This commit is contained in:
Loz Calver 2015-10-07 15:12:00 +01:00
parent 8947bb0245
commit 7a81372294
2 changed files with 38 additions and 0 deletions

View File

@ -1786,6 +1786,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$extraFields = $this->manyManyExtraFieldsForComponent($componentName) ?: array(); $extraFields = $this->manyManyExtraFieldsForComponent($componentName) ?: array();
$result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields); $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields);
// Store component data in query meta-data
$result = $result->alterDataQuery(function($query) use ($extraFields) {
$query->setQueryParam('Component.ExtraFields', $extraFields);
});
if($this->model) $result->setDataModel($this->model); if($this->model) $result->setDataModel($this->model);
$this->extend('updateManyManyComponents', $result); $this->extend('updateManyManyComponents', $result);
@ -2683,6 +2689,28 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function castingHelper($field) {
if ($fieldSpec = $this->db($field)) {
return $fieldSpec;
}
// many_many_extraFields aren't presented by db(), so we check if the source query params
// provide us with meta-data for a many_many relation we can inspect for extra fields.
$queryParams = $this->getSourceQueryParams();
if (!empty($queryParams['Component.ExtraFields'])) {
$extraFields = $queryParams['Component.ExtraFields'];
if (isset($extraFields[$field])) {
return $extraFields[$field];
}
}
return parent::castingHelper($field);
}
/** /**
* Returns true if the given field exists in a database column on any of * Returns true if the given field exists in a database column on any of
* the objects tables and optionally look up a dynamic getter with * the objects tables and optionally look up a dynamic getter with

View File

@ -911,6 +911,16 @@ class DataObjectTest extends SapphireTest {
$this->assertEmpty($fields); $this->assertEmpty($fields);
} }
public function testCastingHelper() {
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
$this->assertEquals('Varchar', $team->castingHelper('Title'), 'db field wasn\'t casted correctly');
$this->assertEquals('HTMLVarchar', $team->castingHelper('DatabaseField'), 'db field wasn\'t casted correctly');
$sponsor = $team->Sponsors()->first();
$this->assertEquals('Int', $sponsor->castingHelper('SponsorFee'), 'many_many_extraFields not casted correctly');
}
public function testSummaryFieldsCustomLabels() { public function testSummaryFieldsCustomLabels() {
$team = $this->objFromFixture('DataObjectTest_Team', 'team1'); $team = $this->objFromFixture('DataObjectTest_Team', 'team1');
$summaryFields = $team->summaryFields(); $summaryFields = $team->summaryFields();