mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Fix incorrect merge of associative / non-associative summary fields
Fixes #7696
This commit is contained in:
parent
a2fa9f0943
commit
ed6561d9f5
@ -3337,12 +3337,15 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
*/
|
*/
|
||||||
public function summaryFields()
|
public function summaryFields()
|
||||||
{
|
{
|
||||||
$fields = $this->config()->get('summary_fields');
|
$rawFields = $this->config()->get('summary_fields');
|
||||||
|
|
||||||
// if fields were passed in numeric array,
|
// Merge associative / numeric keys
|
||||||
// convert to an associative array
|
$fields = [];
|
||||||
if ($fields && array_key_exists(0, $fields)) {
|
foreach ($rawFields as $key => $value) {
|
||||||
$fields = array_combine(array_values($fields), array_values($fields));
|
if (is_int($key)) {
|
||||||
|
$key = $value;
|
||||||
|
}
|
||||||
|
$fields[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$fields) {
|
if (!$fields) {
|
||||||
|
@ -2,25 +2,23 @@
|
|||||||
|
|
||||||
namespace SilverStripe\ORM\Tests;
|
namespace SilverStripe\ORM\Tests;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\i18n\i18n;
|
use SilverStripe\i18n\i18n;
|
||||||
|
use SilverStripe\ORM\Connect\MySQLDatabase;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\DataObjectSchema;
|
use SilverStripe\ORM\DataObjectSchema;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
use SilverStripe\ORM\FieldType\DBBoolean;
|
use SilverStripe\ORM\FieldType\DBBoolean;
|
||||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
use SilverStripe\ORM\DataObject;
|
|
||||||
use SilverStripe\ORM\DB;
|
|
||||||
use SilverStripe\ORM\Connect\MySQLDatabase;
|
|
||||||
use SilverStripe\ORM\FieldType\DBPolymorphicForeignKey;
|
use SilverStripe\ORM\FieldType\DBPolymorphicForeignKey;
|
||||||
use SilverStripe\ORM\FieldType\DBVarchar;
|
use SilverStripe\ORM\FieldType\DBVarchar;
|
||||||
use SilverStripe\ORM\ManyManyList;
|
use SilverStripe\ORM\ManyManyList;
|
||||||
use SilverStripe\ORM\Tests\DataObjectTest\Player;
|
use SilverStripe\ORM\Tests\DataObjectTest\Player;
|
||||||
use SilverStripe\ORM\ValidationException;
|
|
||||||
use SilverStripe\View\ViewableData;
|
use SilverStripe\View\ViewableData;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
use ReflectionException;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
|
|
||||||
class DataObjectTest extends SapphireTest
|
class DataObjectTest extends SapphireTest
|
||||||
{
|
{
|
||||||
@ -87,11 +85,11 @@ class DataObjectTest extends SapphireTest
|
|||||||
|
|
||||||
// Test with table required
|
// Test with table required
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
DataObjectTest\TeamComment::class.'.Varchar',
|
DataObjectTest\TeamComment::class . '.Varchar',
|
||||||
$schema->fieldSpec(DataObjectTest\TeamComment::class, 'Name', DataObjectSchema::INCLUDE_CLASS)
|
$schema->fieldSpec(DataObjectTest\TeamComment::class, 'Name', DataObjectSchema::INCLUDE_CLASS)
|
||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
DataObjectTest\TeamComment::class.'.Text',
|
DataObjectTest\TeamComment::class . '.Text',
|
||||||
$schema->fieldSpec(DataObjectTest\TeamComment::class, 'Comment', DataObjectSchema::INCLUDE_CLASS)
|
$schema->fieldSpec(DataObjectTest\TeamComment::class, 'Comment', DataObjectSchema::INCLUDE_CLASS)
|
||||||
);
|
);
|
||||||
$dbFields = $schema->fieldSpecs(DataObjectTest\ExtendedTeamComment::class);
|
$dbFields = $schema->fieldSpecs(DataObjectTest\ExtendedTeamComment::class);
|
||||||
@ -127,8 +125,8 @@ class DataObjectTest extends SapphireTest
|
|||||||
// Values can be an array...
|
// Values can be an array...
|
||||||
$player = new DataObjectTest\Player(
|
$player = new DataObjectTest\Player(
|
||||||
array(
|
array(
|
||||||
'FirstName' => 'James',
|
'FirstName' => 'James',
|
||||||
'Surname' => 'Smith'
|
'Surname' => 'Smith'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -278,7 +276,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$comment1 = DataObject::get_one(
|
$comment1 = DataObject::get_one(
|
||||||
DataObjectTest\TeamComment::class,
|
DataObjectTest\TeamComment::class,
|
||||||
array(
|
array(
|
||||||
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
||||||
),
|
),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
@ -287,7 +285,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$comment2 = DataObject::get_one(
|
$comment2 = DataObject::get_one(
|
||||||
DataObjectTest\TeamComment::class,
|
DataObjectTest\TeamComment::class,
|
||||||
array(
|
array(
|
||||||
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
||||||
),
|
),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
@ -297,7 +295,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$comment1 = DataObject::get_one(
|
$comment1 = DataObject::get_one(
|
||||||
DataObjectTest\TeamComment::class,
|
DataObjectTest\TeamComment::class,
|
||||||
array(
|
array(
|
||||||
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
@ -306,7 +304,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$comment2 = DataObject::get_one(
|
$comment2 = DataObject::get_one(
|
||||||
DataObjectTest\TeamComment::class,
|
DataObjectTest\TeamComment::class,
|
||||||
array(
|
array(
|
||||||
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
@ -338,7 +336,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$subteam1 = DataObject::get_one(
|
$subteam1 = DataObject::get_one(
|
||||||
strtolower(DataObjectTest\SubTeam::class),
|
strtolower(DataObjectTest\SubTeam::class),
|
||||||
array(
|
array(
|
||||||
'"DataObjectTest_Team"."Title"' => 'Subteam 1'
|
'"DataObjectTest_Team"."Title"' => 'Subteam 1'
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
@ -705,8 +703,8 @@ class DataObjectTest extends SapphireTest
|
|||||||
$obj->getChangedFields(true, DataObject::CHANGE_VALUE),
|
$obj->getChangedFields(true, DataObject::CHANGE_VALUE),
|
||||||
array(
|
array(
|
||||||
'FirstName' => array(
|
'FirstName' => array(
|
||||||
'before'=>'Captain',
|
'before' => 'Captain',
|
||||||
'after'=>'Captain-changed',
|
'after' => 'Captain-changed',
|
||||||
'level' => DataObject::CHANGE_VALUE
|
'level' => DataObject::CHANGE_VALUE
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
@ -1190,15 +1188,13 @@ class DataObjectTest extends SapphireTest
|
|||||||
$summaryFields = $team->summaryFields();
|
$summaryFields = $team->summaryFields();
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'Custom Title',
|
[
|
||||||
$summaryFields['Title'],
|
'Title' => 'Custom Title',
|
||||||
'Custom title is preserved'
|
'Title.UpperCase' => 'Title',
|
||||||
);
|
'Captain.ShirtNumber' => 'Captain\'s shirt number',
|
||||||
|
'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team',
|
||||||
$this->assertEquals(
|
],
|
||||||
'Captain\'s shirt number',
|
$summaryFields
|
||||||
$summaryFields['Captain.ShirtNumber'],
|
|
||||||
'Custom title on relation is preserved'
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1211,10 +1207,10 @@ class DataObjectTest extends SapphireTest
|
|||||||
|
|
||||||
$team1->update(
|
$team1->update(
|
||||||
array(
|
array(
|
||||||
'DatabaseField' => 'Something',
|
'DatabaseField' => 'Something',
|
||||||
'Captain.FirstName' => 'Jim',
|
'Captain.FirstName' => 'Jim',
|
||||||
'Captain.Email' => 'jim@example.com',
|
'Captain.Email' => 'jim@example.com',
|
||||||
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1242,8 +1238,8 @@ class DataObjectTest extends SapphireTest
|
|||||||
|
|
||||||
$team1->update(
|
$team1->update(
|
||||||
array(
|
array(
|
||||||
'Captain.FirstName' => 'Jim',
|
'Captain.FirstName' => 'Jim',
|
||||||
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
/* Test that the captain ID has been updated */
|
/* Test that the captain ID has been updated */
|
||||||
@ -1459,8 +1455,8 @@ class DataObjectTest extends SapphireTest
|
|||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
$this->expectExceptionMessage('Controller is not a valid subclass of DataObject');
|
$this->expectExceptionMessage('Controller is not a valid subclass of DataObject');
|
||||||
/**
|
/**
|
||||||
* @skipUpgrade
|
* @skipUpgrade
|
||||||
*/
|
*/
|
||||||
$dataObject->newClassInstance('Controller');
|
$dataObject->newClassInstance('Controller');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1577,7 +1573,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$teamExtraFields = $team->manyManyExtraFields();
|
$teamExtraFields = $team->manyManyExtraFields();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
array(
|
||||||
'Players' => array('Position' => 'Varchar(100)')
|
'Players' => array('Position' => 'Varchar(100)')
|
||||||
),
|
),
|
||||||
$teamExtraFields
|
$teamExtraFields
|
||||||
);
|
);
|
||||||
@ -1587,8 +1583,8 @@ class DataObjectTest extends SapphireTest
|
|||||||
$teamExtraFields = $subTeam->manyManyExtraFields();
|
$teamExtraFields = $subTeam->manyManyExtraFields();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
array(
|
||||||
'Players' => array('Position' => 'Varchar(100)'),
|
'Players' => array('Position' => 'Varchar(100)'),
|
||||||
'FormerPlayers' => array('Position' => 'Varchar(100)')
|
'FormerPlayers' => array('Position' => 'Varchar(100)')
|
||||||
),
|
),
|
||||||
$teamExtraFields
|
$teamExtraFields
|
||||||
);
|
);
|
||||||
@ -1598,7 +1594,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$teamExtraFields,
|
$teamExtraFields,
|
||||||
array(
|
array(
|
||||||
'Position' => 'Varchar(100)'
|
'Position' => 'Varchar(100)'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1607,7 +1603,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$playerExtraFields,
|
$playerExtraFields,
|
||||||
array(
|
array(
|
||||||
'Position' => 'Varchar(100)'
|
'Position' => 'Varchar(100)'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1797,9 +1793,9 @@ class DataObjectTest extends SapphireTest
|
|||||||
$company = new DataObjectTest\Company();
|
$company = new DataObjectTest\Company();
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array (
|
array(
|
||||||
'CurrentStaff' => DataObjectTest\Staff::class,
|
'CurrentStaff' => DataObjectTest\Staff::class,
|
||||||
'PreviousStaff' => DataObjectTest\Staff::class
|
'PreviousStaff' => DataObjectTest\Staff::class
|
||||||
),
|
),
|
||||||
$company->hasMany(),
|
$company->hasMany(),
|
||||||
'has_many strips field name data by default.'
|
'has_many strips field name data by default.'
|
||||||
@ -1812,16 +1808,16 @@ class DataObjectTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array (
|
array(
|
||||||
'CurrentStaff' => DataObjectTest\Staff::class.'.CurrentCompany',
|
'CurrentStaff' => DataObjectTest\Staff::class . '.CurrentCompany',
|
||||||
'PreviousStaff' => DataObjectTest\Staff::class.'.PreviousCompany'
|
'PreviousStaff' => DataObjectTest\Staff::class . '.PreviousCompany'
|
||||||
),
|
),
|
||||||
$company->hasMany(false),
|
$company->hasMany(false),
|
||||||
'has_many returns field name data when $classOnly is false.'
|
'has_many returns field name data when $classOnly is false.'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
DataObjectTest\Staff::class.'.CurrentCompany',
|
DataObjectTest\Staff::class . '.CurrentCompany',
|
||||||
DataObject::getSchema()->hasManyComponent(DataObjectTest\Company::class, 'CurrentStaff', false),
|
DataObject::getSchema()->hasManyComponent(DataObjectTest\Company::class, 'CurrentStaff', false),
|
||||||
'has_many returns field name data on single records when $classOnly is false.'
|
'has_many returns field name data on single records when $classOnly is false.'
|
||||||
);
|
);
|
||||||
@ -1897,7 +1893,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
public function testBelongsTo()
|
public function testBelongsTo()
|
||||||
{
|
{
|
||||||
$company = new DataObjectTest\Company();
|
$company = new DataObjectTest\Company();
|
||||||
$ceo = new DataObjectTest\CEO();
|
$ceo = new DataObjectTest\CEO();
|
||||||
|
|
||||||
$company->Name = 'New Company';
|
$company->Name = 'New Company';
|
||||||
$company->write();
|
$company->write();
|
||||||
@ -1948,7 +1944,7 @@ class DataObjectTest extends SapphireTest
|
|||||||
public function testBelongsToPolymorphic()
|
public function testBelongsToPolymorphic()
|
||||||
{
|
{
|
||||||
$company = new DataObjectTest\Company();
|
$company = new DataObjectTest\Company();
|
||||||
$ceo = new DataObjectTest\CEO();
|
$ceo = new DataObjectTest\CEO();
|
||||||
|
|
||||||
$company->write();
|
$company->write();
|
||||||
$ceo->write();
|
$ceo->write();
|
||||||
|
@ -59,7 +59,7 @@ class Team extends DataObject implements TestOnly
|
|||||||
);
|
);
|
||||||
|
|
||||||
private static $summary_fields = array(
|
private static $summary_fields = array(
|
||||||
'Title' => 'Custom Title',
|
'Title', // Overridden by Team_Extension
|
||||||
'Title.UpperCase' => 'Title',
|
'Title.UpperCase' => 'Title',
|
||||||
'Captain.ShirtNumber' => 'Captain\'s shirt number',
|
'Captain.ShirtNumber' => 'Captain\'s shirt number',
|
||||||
'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team'
|
'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team'
|
||||||
|
@ -7,6 +7,10 @@ use SilverStripe\ORM\DataExtension;
|
|||||||
|
|
||||||
class Team_Extension extends DataExtension implements TestOnly
|
class Team_Extension extends DataExtension implements TestOnly
|
||||||
{
|
{
|
||||||
|
private static $summary_fields = [
|
||||||
|
'Title' => 'Custom Title', // override non-associative 'Title'
|
||||||
|
];
|
||||||
|
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
'ExtendedDatabaseField' => 'Varchar'
|
'ExtendedDatabaseField' => 'Varchar'
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user