Web Development

The Yii Framework presents a number of ways to check if a record exists in a database table:

Method1: Exists

This method uses the exists() method

$criteria = new CDbCriteria();
$criteria->condition = 'email=:email';
$criteria->params = array(':email'=>$email);
if (User::model()->exists($criteria)) {
    $message = 'The record doesn\'t exist';
} else {
    $message = 'The record exist';
}

Method 2: Check if null

This method checks for the result of the ActiveRecord::find method

$criteria = new CDbCriteria();
$criteria->condition = 'email=:email';
$criteria->params = array(':email'=>$email);
if (User::model()->find($criteria) == null) {
    $message = 'The record doesn\'t exist';
} else {
    $message = 'The record exist';
}

I would recommend the first method. However, the second method is not a bad option. This is usually seen in code generated by Yii Framework’s crud generator .