Due to the risk of being beaten around my head and shoulders for offering a solution other than CakePHP, let me introduce the following.
Create a unique index in your database on any column that you need.
The standard SQL syntax for this is:
create unique index {IndexName} on {Table} ({Column}, {Column}, ...)
Place the command "$ this-> Model-> save ()" inside the try / catch block. In the catch block, check the exception for the error code. In MySQL, a unique key violation is error code 23000, but you must also be prepared for other possible errors.
It is quick and easy and does not require counting the brackets of the array.
In any case, you should always place the database access code inside the try / catch block. Part of exception handling should include logging any unexpected error messages. You cannot expect CakePHP to do everything for you.
Uncaalby
source share