/** * {@inheritdoc} */ protected function createSchema() { $id = $this->getId(); $this->{$id} = $this->getIdType(); $this->variable = DataType::string(255); $this->value = DataType::text(); $this->setPrimaryKey($id, 'variable'); }
protected function createSchema() { $this->addAutoIncrementId(); $this->author = DataType::string(20, true); $this->message = DataType::text(); $this->addTimeStamps(); $this->addIndex('created', 'created'); }
protected function createSchema() { $this->addAutoIncrementId(); $this->title = DataType::string(255); $this->content = DataType::text(); $this->addTimestamps(); $this->addIndex('created', 'created'); }
/** * Construct model. * @param string $name Name of model. */ public function __construct($name = 'Extension') { parent::__construct($name); $this->addField('canonicalName', tr('Canonical name'), DataType::string()); $this->addField('name', tr('Name'), DataType::string()); $this->addField('version', tr('Version'), DataType::string()); $this->addField('description', tr('Description'), DataType::text()); $this->addField('enabled', tr('Enabled'), DataType::boolean()); }
/** * Construct migration runner. */ public function __construct(\Jivoo\Store\Document $config) { $this->config = $config; $this->config->defaults = array('automigrate' => false, 'silent' => false, 'mtimes' => array()); // Initialize SchemaRevision schema $this->definition = new \Jivoo\Data\DefinitionBuilder(); $this->definition->revision = DataType::string(255); $this->definition->setPrimaryKey('revision'); }
protected function createSchema() { $this->addAutoIncrementId(); $this->postId = DataType::integer(DataType::UNSIGNED); $this->author = DataType::string(255); $this->content = DataType::text(); $this->addTimeStamps(); $this->addIndex('postId', 'postId'); }
/** * Construct file model. */ public function __construct() { parent::__construct('File'); $this->addField('path', tr('Path'), DataType::string()); $this->addField('name', tr('Name'), DataType::string()); $this->addField('type', tr('Type'), DataType::enum(array('directory', 'file'))); $this->addField('size', tr('Size'), DataType::integer(DataType::UNSIGNED)); $this->addField('modified', tr('Modified'), DataType::dateTime()); $this->addField('created', tr('Created'), DataType::dateTime()); }
/** * {@inheritdoc} */ public function before() { $this->configForm = new Form('Configure'); $this->configForm->addString('name', tr('Application name')); $this->configForm->addString('version', tr('Version')); $this->configForm->addField('modules', DataType::object(), tr('Modules')); $this->view->data->availableModules = $this->getModules(); $this->view->data->configForm = $this->configForm; $this->view->data->title = tr('Configure application'); return null; }
/** * {@inheritdoc} */ public function __get($property) { if ($property === 'values') { return $this->values; } if ($property === 'placeholder') { if (!isset($this->class)) { throw new InvalidPropertyException(tr('Invalid use of anonymous enum type')); } return '%' . $this->class; } return parent::__get($property); }
protected function createSchema() { $this->addAutoIncrementId(); // Autoincrementing INT id $this->username = DataType::string(255); // Username VARCHAR(255) $this->password = DataType::string(255); // Password VARCHAR(255) $this->addtimeStamps(); // Timestamps: 'created' and 'updated' $this->addUnique('username', 'username'); // A unique index on the username field }
/** * {@inheritdoc} */ protected function init() { $this->config->defaults = array('automigrate' => false, 'silent' => false, 'mtimes' => array()); // Initialize SchemaRevision schema $this->schema = new SchemaBuilder('SchemaRevision'); $this->schema->revision = DataType::string(255); $this->schema->setPrimaryKey('revision'); if (isset($this->app->manifest['migrations'])) { foreach ($this->app->manifest['migrations'] as $name) { $this->attachDatabase($name, $this->p('app', 'migrations/' . $name)); } } else { if (isset($this->m->Databases->default)) { $this->attachDatabase('default', $this->p('app', 'migrations')); } } }
/** * {@inheritdoc} */ public function before() { parent::before(); if (isset($this->request->cookies['data-table-per-page'])) { $this->object->rowsPerPage = intval($this->request->cookies['data-table-per-page']); } if (isset($this->request->cookies['data-table-density'])) { $this->object->density = $this->request->cookies['data-table-density']; } else { $this->object->density = 'medium'; } $tableSettings = new Form('tableSettings'); $tableSettings->addField('perPage', DataType::integer(DataType::UNSIGNED)); $tableSettings->__set('perPage', $this->object->rowsPerPage); $tableSettings->addField('density', DataType::enum(array('low', 'medium', 'high'))); $tableSettings->__set('density', $this->object->density); $this->viewData['tableSettings'] = $tableSettings; }
/** * Set up maintenance user and install lock. * @param string $data POST data if any. * @return \Jivoo\Routing\Response|string Response. */ public function configure($data = null) { $this->viewData['title'] = tr('Configure maintenance user'); $form = new Form('user'); $form->addField('username', DataType::string(), tr('Username')); $form->addField('password', DataType::string(), tr('Password')); $form->addField('confirmPassword', DataType::string(), tr('Confirm password')); $form->username = '******'; if (isset($data)) { $form->addData($data['user']); if ($form->isValid()) { if ($form->password !== $form->confirmPassword) { $form->addError('password', tr('The two passwords are not identical.')); } else { $auth = $this->m->Setup->getAuth(); $password = $auth->passwordHasher->hash($form->password); $this->m->Setup->lock($form->username, $password); return $this->next(); } } } $this->viewData['user'] = $form; return $this->render(); }
/** * Constructor * @param string $name Name of schema */ public function __construct($name) { $this->name = $name; $this->text = DataType::text(true); }
/** * Add a text field to form. * @param string $field Field name. * @param string $label Field label, if not set the field name will be used. * @param bool $required Whether or not the field is required. */ public function addText($field, $label = null, $required = true) { $this->addField($field, DataType::text(), $label, $required); }
/** * Convert output of PRAGMA to DataType. * @param array $row Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { if (preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? */i', $row['type'], $matches) !== 1) { throw new TypeException(tr('Cannot read type "%1" for column: %2', $row['type'], $row['name'])); } $actualType = strtolower($matches[1]); $length = isset($matches[3]) ? $matches[3] : 0; $null = (isset($row['notnull']) and $row['notnull'] != '1'); $default = null; if (isset($row['dflt_value'])) { $default = stripslashes(preg_replace('/^\'|\'$/', '', $row['dflt_value'])); } switch ($actualType) { case 'integer': return DataType::integer(DataType::BIG, $null, isset($default) ? intval($default) : null); case 'real': return DataType::float($null, isset($default) ? floatval($default) : null); case 'text': return DataType::text($null, $default); case 'blob': return DataType::binary($null, $default); } throw new TypeException(tr('Unsupported SQLite type for column: %1', $row['name'])); }
/** * Substitute and encode variables in an expression. * * Placeholders (see also {@see DataType::fromPlaceHolder()}: * <code> * true // Boolean true * false // Boolean false * {AnyModelName} // A model name * [anyFieldName] // A column/field name * "any string" // A string * ? // Any scalar value. * %m %model // A table/model object or name * %c %column %field // A column/field name * %_ // A placeholder placeholder, can also be a type, e.g. where(..., 'id = %_', $type, $value) * %i %int %integer // An integer value * %f %float // A floating point value * %s %str %string // A string * %t $text // Text * %b %bool %boolean // A boolean value * %date // A date value * %d %datetime // A date/time value * %n %bin %binary // A binary object * %AnyEnumClassName // An enum value of that class * %anyPlaceholder() // A tuple of values * </code> * * @param string|Condition $format Expression format, use placeholders instead of values. * @param mixed[] $vars List of values to replace placeholders with. * @param Quoter $quoter Quoter object for quoting identifieres and literals. * @return string The interpolated expression. */ public static function interpolate($format, $vars, Quoter $quoter) { if ($format instanceof self) { return $format->toString($quoter); } assume(is_string($format)); $boolean = DataType::boolean(); $true = $quoter->quoteLiteral($boolean, true); $false = $quoter->quoteLiteral($boolean, false); $format = preg_replace('/\\btrue\\b/i', $true, $format); $format = preg_replace('/\\bfalse\\b/i', $false, $format); $string = DataType::text(); $format = preg_replace_callback('/"((?:[^"\\\\]|\\\\.)*)"|\\{(.+?)\\}|\\[(.+?)\\]/', function ($matches) use($quoter, $string) { if (isset($matches[3])) { return $quoter->quoteField($matches[3]); } else { if (isset($matches[2])) { return $quoter->quoteModel($matches[2]); } else { return $quoter->quoteLiteral($string, stripslashes($matches[1])); } } }, $format); $i = 0; return preg_replace_callback('/((\\?)|%([a-z_\\\\]+))(\\(\\))?/i', function ($matches) use($vars, &$i, $quoter) { $value = $vars[$i]; $i++; $type = null; if (isset($matches[3]) and $matches[3] == '_') { if (!is_string($value)) { assume($value instanceof DataType); $value = $value->placeholder; } $matches[3] = ltrim($value, '%'); $value = $vars[$i]; $i++; } if (isset($matches[3]) and ($matches[3] == 'm' or $matches[3] == 'model')) { if (!is_string($value)) { assume($value instanceof BasicModel); $value = $value->getName(); } return $quoter->quoteModel($value); } if (isset($matches[3]) and ($matches[3] == 'c' or $matches[3] == 'column' or $matches[3] == 'field')) { assume(is_string($value)); return $quoter->quoteField($value); } if (isset($matches[3]) and $matches[3] != '()') { $type = DataType::fromPlaceholder($matches[3]); } if (!isset($type)) { $type = DataType::detectType($value); } if (isset($matches[4]) or isset($matches[3]) and $matches[3] == '()') { assume(is_array($value)); foreach ($value as $key => $v) { $value[$key] = $quoter->quoteLiteral($type, $v); } return '(' . implode(', ', $value) . ')'; } return $quoter->quoteLiteral($type, $value); }, $format); }
/** * Add created and updated timestamps to schema. * @param string $created Created field name. * @param string $updated Updated field name. */ public function addTimestamps($created = 'created', $updated = 'updated') { if (!$this->_readOnly) { $this->{$created} = DataType::dateTime(); $this->{$updated} = DataType::dateTime(); } }
/** * Join with and count the content of an associated collection (associated * using either "hasMany" or "hasAndBelongsToMany"). * @param string $association Name of association. * @param ReadSelection $selection Optional selection. * @return ReadSelection Resulting selection. */ public function withCount($association, ReadSelection $selection = null) { if (!isset($selection)) { $selection = new SelectionBuilder($this); } if (!isset($this->associations)) { $this->createAssociations(); } if (!isset($this->associations[$association])) { throw new InvalidAssociationException(tr('Unknown association: %1', $association)); } $field = $association; $association = $this->associations[$field]; $other = $association['model']; $thisKey = $association['thisKey']; $otherKey = $association['otherKey']; $id = $this->primaryKey; $otherId = $other->primaryKey; if (isset($association['join'])) { $join = $association['join']; $otherPrimary = $association['otherPrimary']; $selection = $selection->leftJoin($join, where('J.%c = %m.%c', $thisKey, $this->name, $id), 'J'); $condition = where('%c.%c = J.%c', $field, $otherId, $otherKey); $count = where('COUNT(J.%c)', $otherKey); } else { $condition = where('%c.%c = %m.%c', $field, $thisKey, $this->name, $id); $count = where('COUNT(%c.%c)', $field, $thisKey); } if (isset($association['condition'])) { $condition = $condition->and($association['condition']); } $selection = $selection->leftJoin($other, $condition, $field); $selection->groupBy(where('%m.%c', $this->name, $id)); return $selection->with($field . '_count', $count, DataType::integer()); }
/** * Convert output of SHOW COLUMN to DataType. * @param array $row Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { $null = $row['is_nullable'] != 'NO'; $default = null; if (isset($row['column_default'])) { $default = $row['column_default']; } $type = $row['data_type']; if (strpos($type, 'int') !== false) { $intFlags = 0; if (preg_match('/^nextval\\(/', $default) === 1) { $intFlags = DataType::AUTO_INCREMENT; $default = null; } else { if (isset($default)) { $default = intval($default); } } if (strpos($type, 'bigint') !== false) { return DataType::integer($intFlags | DataType::BIG, $null, $default); } if (strpos($type, 'smallint') !== false) { return DataType::integer($intFlags | DataType::SMALL, $null, $default); } return DataType::integer($intFlags, $null, $default); } if (strpos($type, 'double') !== false) { return DataType::float($null, isset($default) ? floatval($default) : null); } if (strpos($type, 'bool') !== false) { return DataType::boolean($null, isset($default) ? boolval($default) : null); } if (preg_match("/^'(.*)'::[a-z ]+\$/", $default, $matches) === 1) { $default = $matches[1]; } else { $default = null; } if (strpos($type, 'character') !== false) { $length = $row['character_maximum_length']; return DataType::string($length, $null, $default); } if (strpos($type, 'date') !== false) { return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'timestamp') !== false) { return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'text') !== false) { return DataType::text($null, $default); } throw new TypeException(tr('Unsupported PostgreSQL type "%1" for column: %2', $row['data_type'], $row['column_name'])); }
/** * Construct model. * @param string $name Name of model. */ public function __construct($name = 'Theme') { parent::__construct($name); $this->addField('screenshot', tr('Screenshot'), DataType::string()); }
/** * Convert output of SHOW COLUMN to DataType. * @param array $row Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { $null = (isset($row['Null']) and $row['Null'] != 'NO'); $default = null; if (isset($row['Default'])) { $default = $row['Default']; } if (preg_match('/enum\\((.+)\\)/i', $row['Type'], $matches) === 1) { preg_match_all('/\'([^\']+)\'/', $matches[1], $matches); $values = $matches[1]; return DataType::enum($values, $null, $default); } preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? *(unsigned)? *?/i', $row['Type'], $matches); $actualType = strtolower($matches[1]); $length = isset($matches[3]) ? intval($matches[3]) : 0; $intFlags = 0; if (isset($matches[4])) { $intFlags |= DataType::UNSIGNED; } if (strpos($row['Extra'], 'auto_increment') !== false) { $intFlags |= DataType::AUTO_INCREMENT; } switch ($actualType) { case 'bigint': $intFlags |= DataType::BIG; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'smallint': $intFlags |= DataType::SMALL; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'tinyint': $intFlags |= DataType::TINY; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'int': return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'double': return DataType::float($null, isset($default) ? floatval($default) : null); case 'varchar': return DataType::string($length, $null, $default); case 'blob': return DataType::binary($null, $default); case 'date': return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null); case 'datetime': return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null); case 'text': return DataType::text($null, $default); } throw new TypeException(tr('Unsupported MySQL type for column: %1', $row['Field'])); }