public function createColumn(&$column) { $c = new Column(); $c->inflectedName = Inflector::instance()->variablize($column['field']); $c->name = $column['field']; $c->nullable = $column['not_nullable'] ? false : true; $c->pk = $column['pk'] ? true : false; $c->autoIncrement = false; if (substr($column['type'], 0, 9) == 'timestamp') { $c->rawType = 'datetime'; $c->length = 19; } elseif ($column['type'] == 'date') { $c->rawType = 'date'; $c->length = 10; } else { preg_match('/^([A-Za-z0-9_]+)(\\(([0-9]+(,[0-9]+)?)\\))?/', $column['type'], $matches); $c->rawType = count($matches) > 0 ? $matches[1] : $column['type']; $c->length = count($matches) >= 4 ? intval($matches[3]) : intval($column['attlen']); if ($c->length < 0) { $c->length = null; } } $c->mapRawType(); if ($column['default']) { preg_match('/^nextval\\(\'(.*)\'\\)$/', $column['default'], $matches); if (count($matches) == 2) { $c->sequence = $matches[1]; } else { $c->default = $c->cast($column['default'], $this); } } return $c; }
public function create_column(&$column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['field']); $c->name = $column['field']; $c->nullable = $column['null'] === 'YES' ? true : false; $c->pk = $column['key'] === 'PRI' ? true : false; $c->auto_increment = $column['extra'] === 'auto_increment' ? true : false; if ($column['type'] == 'timestamp' || $column['type'] == 'datetime') { $c->raw_type = 'datetime'; $c->length = 19; } elseif ($column['type'] == 'date') { $c->raw_type = 'date'; $c->length = 10; } else { preg_match('/^([A-Za-z0-9_]+)(\\(([0-9]+(,[0-9]+)?)\\))?/', $column['type'], $matches); $c->raw_type = count($matches) > 0 ? $matches[1] : $column['type']; if (count($matches) >= 4) { $c->length = intval($matches[3]); } } $c->map_raw_type(); $c->default = $c->cast($column['default']); return $c; }
public function load(Model $model) { $keys = array(); $inflector = Inflector::instance(); foreach ($this->foreign_key as $key) { $keys[] = $inflector->variablize($key); } if (!($conditions = $this->create_conditions_from_keys($model, $this->primary_key, $keys))) { return null; } $options = $this->unset_non_finder_options($this->options); $options['conditions'] = $conditions; $class = $this->class_name; return $class::first($options); }
private function set_table_name() { if (($table = $this->class->getStaticPropertyValue('table', null)) || ($table = $this->class->getStaticPropertyValue('table_name', null))) { $this->table = $table; } else { // infer table name from the class name $this->table = Inflector::instance()->tableize($this->class->getName()); // strip namespaces from the table name if any $parts = explode('\\', $this->table); $this->table = $parts[count($parts) - 1]; } if (($db = $this->class->getStaticPropertyValue('db', null)) || ($db = $this->class->getStaticPropertyValue('db_name', null))) { $this->db_name = $db; } }
public function create_column($column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['name']); $c->name = $column['name']; $c->nullable = $column['notnull'] ? false : true; $c->pk = $column['pk'] ? true : false; $c->auto_increment = $column['type'] == 'INTEGER' && $c->pk; $column['type'] = preg_replace('/ +/', ' ', $column['type']); $column['type'] = str_replace(array('(', ')'), ' ', $column['type']); $column['type'] = Utils::squeeze(' ', $column['type']); $matches = explode(' ', $column['type']); if (count($matches) > 0) { $c->raw_type = strtolower($matches[0]); if (count($matches) > 1) { $c->length = intval($matches[1]); } } $c->map_raw_type(); if ($c->type == Column::DATETIME) { $c->length = 19; } elseif ($c->type == Column::DATE) { $c->length = 10; } // From SQLite3 docs: The value is a signed integer, stored in 1, 2, 3, 4, 6, // or 8 bytes depending on the magnitude of the value. // so is it ok to assume it's possible an int can always go up to 8 bytes? if ($c->type == Column::INTEGER && !$c->length) { $c->length = 8; } $c->default = $c->cast($column['dflt_value']); return $c; }
/** * Enables the use of dynamic finders. * * Dynamic finders are just an easy way to do queries quickly without having to * specify an options array with conditions in it. * * <code> * SomeModel::findByFirstName('Tito'); * SomeModel::findByFirstNameAndLastName('Tito','the Grief'); * SomeModel::findByFirstNameOrLastName('Tito','the Grief'); * SomeModel::findAllByLastName('Smith'); * SomeModel::countByName('Bob') * SomeModel::countByNameOrState('Bob','VA') * SomeModel::countByNameAndState('Bob','VA') * </code> * * You can also create the model if the find call returned no results: * * <code> * Person::findOrCreateByName('Tito'); * * # would be the equivalent of * if (!Person::findByName('Tito')) * Person::create(array('Tito')); * </code> * * Some other examples of find_or_create_by: * * <code> * Person::findOrCreateByNameAndId('Tito',1); * Person::findOrCreateByNameAndId(array('name' => 'Tito', 'id' => 1)); * </code> * * @param string $method Name of method * @param mixed $args Method args * @return Model * @throws {@link ActiveRecordException} if invalid query * @see find */ public static function __callStatic($method, $args) { // TODO: Remove this bad fix, rewrite the code below properly. $method = strtolower(Inflector::instance()->underscorify($method)); $options = static::extractAndValidateOptions($args); $create = false; if (substr($method, 0, 17) == 'find_or_create_by') { $attributes = substr($method, 17); // can't take any finders with OR in it when doing a find_or_create_by if (strpos($attributes, '_or_') !== false) { throw new ActiveRecordException("Cannot use OR'd attributes in find_or_create_by"); } $create = true; $method = 'find_by' . substr($method, 17); } if (substr($method, 0, 7) === 'find_by') { $attributes = substr($method, 8); $options['conditions'] = SQLBuilder::createConditionsFromUnderscoredString(static::connection(), $attributes, $args, static::$aliasAttribute); if (!($ret = static::find('first', $options)) && $create) { return static::create(SQLBuilder::createHashFromUnderscoredString($attributes, $args, static::$aliasAttribute)); } return $ret; } elseif (substr($method, 0, 11) === 'find_all_by') { $options['conditions'] = SQLBuilder::createConditionsFromUnderscoredString(static::connection(), substr($method, 12), $args, static::$aliasAttribute); return static::find('all', $options); } elseif (substr($method, 0, 8) === 'count_by') { $options['conditions'] = SQLBuilder::createConditionsFromUnderscoredString(static::connection(), substr($method, 9), $args, static::$aliasAttribute); return static::count($options); } throw new ActiveRecordException("Call to undefined method: {$method}"); }