Skip to content

Instantly share code, notes, and snippets.

@bkudria
Created October 16, 2009 22:07
Show Gist options
  • Save bkudria/212103 to your computer and use it in GitHub Desktop.
Save bkudria/212103 to your computer and use it in GitHub Desktop.
public static function makeIdentifier($string, $entity=null) {
$base_identifier = Model::sanitizeName($string);
if ($entity) {
$new_increment =
Kohana_ORM::factory($entity)
->select(new Kohana_Database_Expression('substring(identifier, -1) + 1 as new_increment'))
->regex('identifier', $base_identifier.'[1-9][0-9]*')
->orderby('new_increment', 'desc')
->limit(1)
->find();
return $base_identifier . ($new_increment->count() == 0 ? '' : $new_increment->current()->new_increment);
} else {
return $base_identifier;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment