【问题标题】:How to define composite primary key in SilverStripe ORM/Dataobject如何在 SilverStripe ORM/Dataobject 中定义复合主键
【发布时间】:2016-05-19 13:40:10
【问题描述】:

SilverStripe 的 DataObject 为我们提供了以下内容:

ID - 主键

但是如何定义复合键(由 2 列或更多列组成的主键)?我搜索了文档,但在任何地方都找不到此信息。

【问题讨论】:

  • Silverstripe 专家请阐明如何在此框架中处理复合主键。

标签: php silverstripe


【解决方案1】:

我不确定主键,但您可以设置唯一索引。它应该给你一个类似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"'
        )
    );
}

使用此代码,如果数据库中已经存在包含这两个值的记录,则无法使用MyField = 'test'MyOtherField = 'othertest' 创建YourDataObject。可以创建一个 YourDataObject,仅将 MyField 作为测试,MyOtherField 作为其他内容。

但是,建议在将其写入数据库之前对其进行检查,因为您会在 ModelAdmin 中收到用户不友好的错误。

documentation复制的代码

【讨论】:

  • 不要将代码放在 ModelAdmin 中,而是放在 DataObject 中。
猜你喜欢
  • 2018-03-17
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 2015-12-23
  • 2018-03-02
  • 2014-06-27
  • 2010-09-18
  • 2014-01-17
相关资源
最近更新 更多