API Allow $summary_fields to support methods on DBFields

This commit is contained in:
Damian Mooyman 2013-06-13 09:41:24 +12:00
parent 88595e38f9
commit be986c6524
4 changed files with 49 additions and 19 deletions

View File

@ -167,13 +167,13 @@ Example: Simple Definition
'ProductCode' => 'Int',
);
private static $summary_fields = array(
'Name',
'ProductCode'
);
'Name',
'ProductCode'
);
}
To include relations in your summaries, you can use a dot-notation.
To include relations or field manipulations in your summaries, you can use a dot-notation.
:::php
class OtherObject extends DataObject {
@ -183,17 +183,37 @@ To include relations in your summaries, you can use a dot-notation.
}
class MyDataObject extends DataObject {
private static $db = array(
'Name' => 'Text'
'Name' => 'Text',
'Description' => 'HTMLText'
);
private static $has_one = array(
'OtherObject' => 'OtherObject'
);
private static $summary_fields = array(
'Name',
'OtherObject.Title'
);
private static $summary_fields = array(
'Name' => 'Name',
'Description.Summary' => 'Description (summary)',
'OtherObject.Title' => 'Other Object Title'
);
}
Non-textual elements (such as images and their manipulations) can also be used in summaries.
:::php
class MyDataObject extends DataObject {
private static $db = array(
'Name' => 'Text'
);
private static $has_one = array(
'HeroImage' => 'Image'
);
private static $summary_fields = array(
'Name' => 'Name,
'HeroImage.CMSThumbnail' => 'Hero Image'
);
}
## Permissions
Models can be modified in a variety of controllers and user interfaces,

View File

@ -2727,12 +2727,20 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$relations = explode('.', $fieldName);
$fieldName = array_pop($relations);
foreach($relations as $relation) {
// Bail if any of the below sets a $component to a null object
if($component instanceof SS_List && !method_exists($component, $relation)) {
$component = $component->relation($relation);
// Just call the method and hope for the best
} else {
// Inspect $component for element $relation
if($component->hasMethod($relation)) {
// Check nested method
$component = $component->$relation();
} elseif($component instanceof SS_List) {
// Select adjacent relation from DataList
$component = $component->relation($relation);
} elseif($component instanceof DataObject
&& ($dbObject = $component->dbObject($relation))
) {
// Select db object
$component = $dbObject;
} else {
user_error("$relation is not a relation/field on ".get_class($component), E_USER_ERROR);
}
}
}

View File

@ -1103,6 +1103,10 @@ class DataObjectTest extends SapphireTest {
$newPlayer = new DataObjectTest_Player();
$this->assertNull($newPlayer->relField('Teams.First.Title'));
// Test that relField works on db field manipulations
$comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment3');
$this->assertEquals("PHIL IS A UNIQUE GUY, AND COMMENTS ON TEAM2" , $comment->relField('Comment.UpperCase'));
}
public function testRelObject() {

View File

@ -20,7 +20,6 @@ DataObjectTest_Player:
player2:
FirstName: Player 2
Teams: =>DataObjectTest_Team.team1,=>DataObjectTest_Team.team2
DataObjectTest_SubTeam:
subteam1:
Title: Subteam 1
@ -33,12 +32,11 @@ DataObjectTest_SubTeam:
ExtendedHasOneRelationship: =>DataObjectTest_Player.player1
subteam3_with_empty_fields:
Title: Subteam 3
DataObjectTest_TeamComment:
comment1:
Name: Joe
Comment: This is a team comment by Joe
Team: =>DataObjectTest_Team.team1
Name: Joe
Comment: This is a team comment by Joe
Team: =>DataObjectTest_Team.team1
comment2:
Name: Bob
Comment: This is a team comment by Bob