Yii 1.1: model-unique-validation

Allows for easy model validation against all columns while excluding those which are passed in via array
2 followers

Summary

Often it is very helpful to test uniqueness of a record/model in the database, but excluding a few columns for one reason or another. Typical cases might be a record which could be entered by more than one individual where there is a database column for that user, userID, and also a datetime field, createdDate, and a primary key, id, all of which are unique. When testing for duplicates it is only going to be valid if these fields are excluded.

It is possible yii already has this functionality now--if this is the case please notify me and I will remove this extension

Typical use case:

// do some work here
$model->attributes = $_POST['SomeClass'];
 
// exclude some columns which cause issues, or are always unique
$excludeColumns = ['id', 'createdDate', 'userID'];
 
if( $model->getExists($excludeColumns) )
     $model->addError('id',"This record is not unique.");
else
     $model->save();

Requirements

  • this class, ActiveRecord, to be placed into somewhere meaningful (protected/components/, for example)
  • any models for which this functionality is desired should extend this class.

Links

Project Page

Be the first person to leave a comment

Please to leave your comment.

Create extension