Skip to content

Instantly share code, notes, and snippets.

@cottonaf
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cottonaf/d134c2776e361a1dff51 to your computer and use it in GitHub Desktop.
Save cottonaf/d134c2776e361a1dff51 to your computer and use it in GitHub Desktop.
<?php
class ActiveRecord extends CActiveRecord
{
/**
* Check if the current record already exists in the database.
*
* $exludeColumns array of attribute names; these columns/attribute names will be excluded from search.
* @return boolean
*
* $arrAttrs is a copy of the $model->attributes array except that each key has an array for a value instead of a string.
* This allows CDbCommandBuilder->createColumnCriteria() to use it as an IN condition.
*/
public function getExists($excludeColumns=array())
{
$arrAttrs = array();
$attrs = $this->attributes;
if( !empty($excludeColumns) )
{
foreach($excludeColumns as $columnName)
{
unset($attrs[$columnName]);
}
foreach($attrs as $name=>$value)
{
// only search it if it is NON NULL and NON EMPTY STRING, otherwise just ignore the column
if( isset($value) && !empty($value) )
$arrAttrs[$name] = array($value);
}
}
if( $this->isNewRecord )
return $this->countByAttributes($arrAttrs) > 0;
return false;
}
}
?>
The MIT License (MIT)
Copyright (c) 2015 Andrew Cotton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@cottonaf
Copy link
Author

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment