Skip to content

Instantly share code, notes, and snippets.

@CurtisHumphrey
Created July 14, 2013 00:51
Show Gist options
  • Save CurtisHumphrey/5992759 to your computer and use it in GitHub Desktop.
Save CurtisHumphrey/5992759 to your computer and use it in GitHub Desktop.
PHP php-activerecord code for checking existence and reusing before creating
//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