Created
July 14, 2013 00:51
-
-
Save CurtisHumphrey/5992759 to your computer and use it in GitHub Desktop.
PHP php-activerecord code for checking existence and reusing before creating
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
//validations | |
static $validates_presence_of = array( //TODO | |
//alterations | |
static $before_create = array('check_existence'); | |
function check_existence() | |
{ | |
$find_by = array(); | |
foreach(self::$validates_presence_of as $key) | |
{ | |
$key = $key[0]; | |
$find_by[$key] = $this->$key; | |
} | |
$result = self::first($find_by); | |
if ($result != null) | |
{ | |
$this->set_attributes($result->attributes()); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment