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', 'ProductCode' => 'Int',
); );
private static $summary_fields = array( private static $summary_fields = array(
'Name', 'Name',
'ProductCode' '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 :::php
class OtherObject extends DataObject { class OtherObject extends DataObject {
@ -183,17 +183,37 @@ To include relations in your summaries, you can use a dot-notation.
} }
class MyDataObject extends DataObject { class MyDataObject extends DataObject {
private static $db = array( private static $db = array(
'Name' => 'Text' 'Name' => 'Text',
'Description' => 'HTMLText'
); );
private static $has_one = array( private static $has_one = array(
'OtherObject' => 'OtherObject' 'OtherObject' => 'OtherObject'
); );
private static $summary_fields = array( private static $summary_fields = array(
'Name', 'Name' => 'Name',
'OtherObject.Title' '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 ## Permissions
Models can be modified in a variety of controllers and user interfaces, 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); $relations = explode('.', $fieldName);
$fieldName = array_pop($relations); $fieldName = array_pop($relations);
foreach($relations as $relation) { foreach($relations as $relation) {
// Bail if any of the below sets a $component to a null object // Inspect $component for element $relation
if($component instanceof SS_List && !method_exists($component, $relation)) { if($component->hasMethod($relation)) {
$component = $component->relation($relation); // Check nested method
// Just call the method and hope for the best
} else {
$component = $component->$relation(); $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(); $newPlayer = new DataObjectTest_Player();
$this->assertNull($newPlayer->relField('Teams.First.Title')); $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() { public function testRelObject() {

View File

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