Cakephp unique validation in 1.2
Friday, September 21st, 2007A little disclaimer: I could be totally wrong because I’m still learning the ins and outs of CakePHP.
I don’t like the current solution for validation of unique fields in the 1.2 CakePHP AppModel mainly because I couldn’t get it to work so I created my own. Sorry about the formatting, I’ve never posted code in my blog before and not being able to properly indent the code isn’t a big concern for me. The code was inspired by the comments between speedmax and cakebaker on his blog.
// YourModel.php
var $validate = array( // validate our username and email
'username' => array('rule' => array('alphaNumeric')),
'email' => array('rule' => array('email'))
);
function beforeSave() {
$check = array('username','email'); // username and email are unique
return $this->checkUnique($check);
}
The code above specifies username and email as unique values and will check for them in the database before we save. Below is my modified app model.
// AppModel.php
class AppModel extends Model {
function checkUnique($myUnique) {
$sql = "";
foreach($myUnique as $name ) {
if($sql!="") $sql.=' OR ';
$sql .= $this->name.".$name=\"".$this->data[$this->name][$name].’”‘;
}
$found = $this->find($sql);
$same = isset($this->id) && $found[$this->name]['id'] == $this->id;
return !$found || $found && $same;
}
}
Now in the controller all you have to do is save your data.
if($this->YourModel->save($this->data)) {
// success
} else {
// failure
}
The code runs save() -> validate() -> beforeSave() -> checkUnique() and wont save to mysql if validate or beforeSave is false. I think the code is a little more strait-forward compared to making countless arrays within arrays.
