Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API Allow $summary_fields to support methods on DBFields

  • Loading branch information...
commit be986c6524af5d121934610c211e35d3b1b99ec5 1 parent 88595e3
@tractorcow tractorcow authored
View
38 docs/en/reference/dataobject.md
@@ -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
18 model/DataObject.php
@@ -2727,12 +2727,20 @@ public function relField($fieldName) {
$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
4 tests/model/DataObjectTest.php
@@ -1103,6 +1103,10 @@ public function testRelField() {
$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
8 tests/model/DataObjectTest.yml
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.