How to define a composite primary key in SilverStripe ORM / Dataobject

SilverStripe DataObject gives us the following:

Identifier - Primary Key

But how do you define a composite key (a primary key consisting of two or more columns)? I searched the documentation and cannot find this information anywhere.

+3
source share
1 answer

I'm not sure about the primary key, but instead you can set a unique index. It should give you a sort of like here .

class YourDataObject extends DataObject
{
    private static $db = [
        'MyField' => 'Varchar',
        'MyOtherField' => 'Varchar'
    ];

    private static $indexes = array(
        'MyIndexName' => array(
            'type' => 'unique', // changed this to unique
            'value' => '"MyField","MyOtherField"'
        )
    );
}

Using this code, it is impossible to create YourDataObjectwith MyField = 'test'and MyOtherField = 'othertest'if there is already an entry with BOTH with these values ​​in the database. You can YourDataObjectonly create with a test MyFieldand MyOtherFieldlike something else.

, , , ModelAdmin.

+2

Source: https://habr.com/ru/post/1686995/


All Articles