Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Fix writeBaseRecord with unique indexes #8831

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,8 +1413,10 @@ protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$ma
// Inserts done one the base table are performed in another step, so the manipulation should instead
// attempt an update, as though it were a normal update.
$manipulation[$table]['command'] = $isNewRecord ? 'insert' : 'update';
$manipulation[$table]['id'] = $this->record['ID'];
$manipulation[$table]['class'] = $class;
if ($this->isInDB()) {
$manipulation[$table]['id'] = $this->record['ID'];
}
}

/**
Expand All @@ -1433,10 +1435,10 @@ protected function writeBaseRecord($baseTable, $now)
}

// Perform an insert on the base table
$insert = new SQLInsert('"' . $baseTable . '"');
$insert
->assign('"Created"', $now)
->execute();
$manipulation = [];
$this->prepareManipulationTable($baseTable, $now, true, $manipulation, $this->baseClass());
DB::manipulate($manipulation);

$this->changed['ID'] = self::CHANGE_VALUE;
$this->record['ID'] = DB::get_generated_id($baseTable);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/php/ORM/DataObjectSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,35 @@ public function testFieldsCanBeIndexedFromFieldSpecs()
'columns' => ['IndexedMoneyCurrency', 'IndexedMoneyAmount']
], $indexes['IndexedMoney']);
}

/**
* Ensure that records with unique indexes can be written
*/
public function testWriteUniqueIndexes()
{
// Create default object
$zeroObject = new AllIndexes();
$zeroObject->Number = 0;
$zeroObject->write();

$this->assertListEquals(
[
['Number' => 0],
],
AllIndexes::get()
);

// Test a new record can be created without clashing with default value
$validObject = new AllIndexes();
$validObject->Number = 1;
$validObject->write();

$this->assertListEquals(
[
['Number' => 0],
['Number' => 1],
],
AllIndexes::get()
);
}
}
5 changes: 5 additions & 0 deletions tests/php/ORM/DataObjectSchemaTest/AllIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBIndexable;

/**
* @property int $Number
* @property string $Content
* @property string $Title
*/
class AllIndexes extends DataObject implements TestOnly
{
private static $table_name = 'DataObjectSchemaTest_AllIndexes';
Expand Down