How to avoid duplicate emails in the database?

3

I'm creating a simple CMS in CakePHP, in order to learn how to use framework. Within my CMS, in the admin part of the site, there is a tab called send emails and in this tab we see all emails that have been registered by the administrator. However at the time of registering an email it allows us to register the same email again and again. How do I eliminate this failure?

    
asked by anonymous 27.07.2014 / 02:40

2 answers

2

Just set 2 rules in the email field.

Within your model, which has this field, it looks like this:

public $validate = array(
// Aqui vai o nome do campo
'email' => array(
    // O nome que você quiser dar na validação do campo
    'rule1Email' => array(
        // Tipo de regra
        // parãmetro true verifica se o host é válido
        'rule' => array('email', true),
        //Sua mensagem de erro para esta regra
        'message' => 'Insira um email válido',
        //Obriga a preencher
        'required' => true,
        //Se quer limitar a apenas alguma action
        'on' => 'create'
    ),
    //Outra regra para o mesmo campo.
    'rule2isUnique' => array(
        //Tipo de regra
        'rule' => 'isUnique',
        //Sua mensagem de erro para esta regra
        'message' => 'Email já cadastrado'
        )
    )
);

Source and other options: link

    
27.07.2014 / 22:54
0

Simply put the field where the email is registered as UNIQUE, if an insert attempts to insert an equal value it will not allow.

ALTER TABLE 'test'.'aplicacao' 
CHANGE COLUMN 'name' 'email' VARCHAR(255) NOT NULL ,
ADD UNIQUE INDEX 'email_UNIQUE' ('email' ASC);

If an insert attempts to register a value that already exists will return error as below

Error Code: 1062. Duplicate entry '[email protected]' for key 'email_UNIQUE'

So you guarantee the bank, the application is another matter.

    
19.08.2014 / 22:07