You can do the uniqueness check using Validation and an already-written callback. This has the advantages of keeping your validation logic together, and of being very concise:
public function rules()
{
return array(
'username' => array(
array(array($this, 'unique'), array(':field', ':value')),
// ...
As simple as that!
I originally answered this question with my own solution, which is slightly different from the pre-rolled version, but now that I know about that obviously I'll use it instead of this:
public function rules()
{
return array(
'username' => array(
// ...
array('Model_User::unique_field', array(':field', ':value', $this->pk())),
),
// ...
);
}
public static function unique_field($field, $value, $user_id = NULL)
{
return (ORM::factory('User')->where($field, '=', $value)->find()->pk() === $user_id);
}