Last active
August 29, 2015 14:21
-
-
Save cottonaf/d134c2776e361a1dff51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Requirements