Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql SilverStripe即使具有唯一索引也会重复条目_Sql_Orm_Silverstripe - Fatal编程技术网

Sql SilverStripe即使具有唯一索引也会重复条目

Sql SilverStripe即使具有唯一索引也会重复条目,sql,orm,silverstripe,Sql,Orm,Silverstripe,在我的CRM中添加具有以下索引的客户记录时,我试图防止重复记录: private static $indexes = array( 'IndexFirstSurName' => array( 'type' => 'unique', 'value' => '"FirstName","Surname"' ) ); 请注意,我从成员扩展了客户,其中名字和姓氏来自: class Customer extends Member 但是

在我的CRM中添加具有以下索引的客户记录时,我试图防止重复记录:

private static $indexes = array(
    'IndexFirstSurName' => array(
        'type' => 'unique', 
        'value' => '"FirstName","Surname"'
    )
);
请注意,我从
成员
扩展了
客户
,其中
名字
姓氏
来自:

class Customer extends Member 

但是SilverStripe仍然允许重复输入
FirstName
姓氏
组合?有人遇到过同样的问题吗?

根据我的经验,即使在使用索引时,仍然需要validate():

public function validate() {
    $result = parent::validate();

    if(Member::get()->filter(array('FirstName' => $this->FirstName, 'Surname' => $this->Surname))->first()) {
        $result->error('First and Surname must be unique for each member.');
    }

    return $result;
}
或者,为了获得更强劲的突破:

public function validate() {
    $result = parent::validate();

    if($member = Member::get()->filter(array('FirstName' => $this->FirstName, 'Surname' => $this->Surname))->first()) {
        if($member->FirstName == $this->FirstName){
            $result->error('Your Surname is fine, please change your First Name.');
        }
        if($member->Surname == $this->Surname){
            $result->error('Your First Name is fine, please change your Surname.');
        }
    }

    return $result;
}

根据我的经验,即使在使用索引时,仍然需要validate():

public function validate() {
    $result = parent::validate();

    if(Member::get()->filter(array('FirstName' => $this->FirstName, 'Surname' => $this->Surname))->first()) {
        $result->error('First and Surname must be unique for each member.');
    }

    return $result;
}
或者,为了获得更强劲的突破:

public function validate() {
    $result = parent::validate();

    if($member = Member::get()->filter(array('FirstName' => $this->FirstName, 'Surname' => $this->Surname))->first()) {
        if($member->FirstName == $this->FirstName){
            $result->error('Your Surname is fine, please change your First Name.');
        }
        if($member->Surname == $this->Surname){
            $result->error('Your First Name is fine, please change your Surname.');
        }
    }

    return $result;
}
请注意,我从会员处扩展了客户的名字和姓氏

我想知道SilverStripe是否正试图在不存在的字段
Customer.FirstName
Customer.lasname
上设置索引。可以尝试通过在实际添加索引的表前面加上前缀来限定列,如下所示:

private static $indexes = array(
    'IndexFirstSurName' => array(
        'type' => 'unique', 
        'value' => '"Member"."FirstName","Member"."Surname"'
    )
);

您也可以考虑装饰<代码>成员<代码>,而不是对它进行子类化。这样,您就不需要以这种方式限定查询片段

请注意,我从会员处扩展了客户的名字和姓氏

我想知道SilverStripe是否正试图在不存在的字段
Customer.FirstName
Customer.lasname
上设置索引。可以尝试通过在实际添加索引的表前面加上前缀来限定列,如下所示:

private static $indexes = array(
    'IndexFirstSurName' => array(
        'type' => 'unique', 
        'value' => '"Member"."FirstName","Member"."Surname"'
    )
);

您也可以考虑装饰<代码>成员<代码>,而不是对它进行子类化。这样,您就不需要以这种方式限定查询片段。

在SilverStripe上扩展
成员的方法是扩展
数据扩展。正如theruss所说,您正试图在表
Customer
上创建一个唯一的索引,其中您可能没有字段
FirstName
names

试试这个

class Customer extends DataExtension
{
    private static $indexes = array(
        'IndexFirstSurName' => array(
            'type' => 'unique', 
            'value' => '"FirstName","Surname"'
        )
    );
}
然后在
config.yml

Member:
  extensions:
    - Customer
现在运行
/dev/build?flush
,您应该会看到正在创建索引


查看有关扩展的更多信息。

在SilverStripe上扩展
成员的方法是扩展
DataExtension
。正如theruss所说,您正试图在表
Customer
上创建一个唯一的索引,其中您可能没有字段
FirstName
names

试试这个

class Customer extends DataExtension
{
    private static $indexes = array(
        'IndexFirstSurName' => array(
            'type' => 'unique', 
            'value' => '"FirstName","Surname"'
        )
    );
}
然后在
config.yml

Member:
  extensions:
    - Customer
现在运行
/dev/build?flush
,您应该会看到正在创建索引


查看有关扩展的更多信息。

这是验证字段的好方法,但我认为Silverstripe唯一索引已变得无用。@muskie9-这不应该是验证错误,并特别提及字段。。。。因此错误出现在该字段旁边?@Barry是的,可以进行额外的逻辑来确定确切的字段,但这将是确保其他记录不共享FirstName/姓氏值的最低要求。如何确定确切的字段并使错误显示在该字段旁边?这是验证字段的一个好方法,但我认为Silverstripe unique index已变得无用。@muskie9-这不应该是验证错误并提及该字段吗明确地因此错误出现在该字段旁边?@Barry是的,可以通过附加逻辑来确定确切的字段,但这将是确保没有其他记录共享FirstName/姓氏值的最低要求。如何确定确切的字段并使错误出现在该字段旁边?