The Man The Man - 6 months ago 25
SQL Question

Silverstripe duplicate entries even with unique index

I'm trying to prevent duplicate records when adding customer records in my crm with the following index,

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


note that I extended Customer from Member were FirstName and Surname came from

class Customer extends Member


But Silverstripe is still allowing duplicate entries of FirstNane and Surname combination? Anyone experienced the same?

Answer

The Man, in my experience a validate() is still needed even when using indexing:

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;
}
Comments