/** * Build the fixtures table schema from the fields property. * * @return void */ protected function _schemaFromFields() { $connection = ConnectionManager::get($this->connection()); $this->_schema = new Table($this->table); foreach ($this->fields as $field => $data) { if ($field === '_constraints' || $field === '_indexes' || $field === '_options') { continue; } $this->_schema->addColumn($field, $data); } if (!empty($this->fields['_constraints'])) { foreach ($this->fields['_constraints'] as $name => $data) { if (!$connection->supportsDynamicConstraints() || $data['type'] !== Table::CONSTRAINT_FOREIGN) { $this->_schema->addConstraint($name, $data); } else { $this->_constraints[$name] = $data; } } } if (!empty($this->fields['_indexes'])) { foreach ($this->fields['_indexes'] as $name => $data) { $this->_schema->addIndex($name, $data); } } if (!empty($this->fields['_options'])) { $this->_schema->options($this->fields['_options']); } }
/** * Build the fixtures table schema from the fields property. * * @return void */ protected function _schemaFromFields() { $this->_schema = new Table($this->table); foreach ($this->fields as $field => $data) { if ($field === '_constraints' || $field === '_indexes' || $field === '_options') { continue; } // Trigger errors on deprecated usage. if (is_array($data) && isset($data['key'])) { $msg = 'Usage of the `key` options in columns is not supported. Try using the upgrade shell to migrate your fixtures.'; $msg .= ' You can download the upgrade shell from https://github.com/cakephp/upgrade.'; trigger_error($msg, E_USER_NOTICE); } $this->_schema->addColumn($field, $data); } if (!empty($this->fields['_constraints'])) { foreach ($this->fields['_constraints'] as $name => $data) { $this->_schema->addConstraint($name, $data); } } if (!empty($this->fields['_indexes'])) { // Trigger errors on deprecated usage. if (empty($data['type'])) { $msg = 'Indexes must define a type. Try using the upgrade shell to migrate your fixtures.'; $msg .= ' You can download the upgrade shell from https://github.com/cakephp/upgrade.'; trigger_error($msg, E_USER_NOTICE); } foreach ($this->fields['_indexes'] as $name => $data) { $this->_schema->addIndex($name, $data); } } if (!empty($this->fields['_options'])) { $this->_schema->options($this->fields['_options']); } }
/** * Build the fixtures table schema from the fields property. * * @return void */ protected function _schemaFromFields() { $this->_schema = new Table($this->table); foreach ($this->fields as $field => $data) { if ($field === '_constraints' || $field === '_indexes' || $field === '_options') { continue; } $this->_schema->addColumn($field, $data); } if (!empty($this->fields['_constraints'])) { foreach ($this->fields['_constraints'] as $name => $data) { $this->_schema->addConstraint($name, $data); } } if (!empty($this->fields['_indexes'])) { foreach ($this->fields['_indexes'] as $name => $data) { $this->_schema->addIndex($name, $data); } } if (!empty($this->fields['_options'])) { $this->_schema->options($this->fields['_options']); } }
/** * Generates a string representation of a schema. * * @param \Cake\Database\Schema\Table $table Table schema * @return string fields definitions */ protected function _generateSchema(Table $table) { $cols = $indexes = $constraints = []; foreach ($table->columns() as $field) { $fieldData = $table->column($field); $properties = implode(', ', $this->_values($fieldData)); $cols[] = " '{$field}' => [{$properties}],"; } foreach ($table->indexes() as $index) { $fieldData = $table->index($index); $properties = implode(', ', $this->_values($fieldData)); $indexes[] = " '{$index}' => [{$properties}],"; } foreach ($table->constraints() as $index) { $fieldData = $table->constraint($index); $properties = implode(', ', $this->_values($fieldData)); $constraints[] = " '{$index}' => [{$properties}],"; } $options = $this->_values($table->options()); $content = implode("\n", $cols) . "\n"; if (!empty($indexes)) { $content .= " '_indexes' => [\n" . implode("\n", $indexes) . "\n ],\n"; } if (!empty($constraints)) { $content .= " '_constraints' => [\n" . implode("\n", $constraints) . "\n ],\n"; } if (!empty($options)) { foreach ($options as &$option) { $option = ' ' . $option; } $content .= " '_options' => [\n" . implode(",\n", $options) . "\n ],\n"; } return "[\n{$content} ]"; }
/** * {@inheritDoc} */ public function createTableSql(Table $table, $columns, $constraints, $indexes) { $content = implode(",\n", array_merge($columns, $constraints, $indexes)); $temporary = $table->temporary() ? ' TEMPORARY ' : ' '; $content = sprintf("CREATE%sTABLE `%s` (\n%s\n)", $temporary, $table->name(), $content); $options = $table->options(); if (isset($options['engine'])) { $content .= sprintf(' ENGINE=%s', $options['engine']); } if (isset($options['charset'])) { $content .= sprintf(' DEFAULT CHARSET=%s', $options['charset']); } if (isset($options['collate'])) { $content .= sprintf(' COLLATE=%s', $options['collate']); } return [$content]; }
<?php /** * Queued Tasks schema file * * @author David Yell <*****@*****.**> * @author MGriesbach@gmail.com */ use Cake\Database\Schema\Table; $t = new Table('queued_tasks'); $t->addColumn('id', ['type' => 'integer', 'length' => 10, 'null' => false, 'default' => null]); $t->addColumn('job_type', ['type' => 'string', 'null' => false, 'length' => 45]); $t->addColumn('data', ['type' => 'text', 'null' => true, 'default' => null]); $t->addColumn('job_group', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]); $t->addColumn('reference', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]); $t->addColumn('created', ['type' => 'datetime', 'null' => true, 'default' => null]); $t->addColumn('notbefore', ['type' => 'datetime', 'null' => true, 'default' => null]); $t->addColumn('fetched', ['type' => 'datetime', 'null' => true, 'default' => null]); $t->addColumn('progress', ['type' => 'float', 'length' => '3,2', 'null' => true, 'default' => null]); $t->addColumn('status', ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null]); $t->addColumn('completed', ['type' => 'datetime', 'null' => true, 'default' => null]); $t->addColumn('failed', ['type' => 'integer', 'null' => false, 'default' => '0', 'length' => 3]); $t->addColumn('failure_message', ['type' => 'text', 'null' => true, 'default' => null]); $t->addColumn('workerkey', ['type' => 'string', 'null' => true, 'length' => 45]); $t->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); $t->options(['collate' => 'utf8_unicode_ci']);
/** * {@inheritDoc} */ public function convertOptionsDescription(Table $table, $row) { $table->options(['engine' => $row['Engine'], 'collation' => $row['Collation']]); }
/** * Gets an schema instance for the given fixture class. * * @param string $fixtureClassName The fixture to be "converted" * @return \Cake\Database\Schema\Table Schema instance */ protected function _prepareSchema($fixtureClassName) { $fixture = new $fixtureClassName(); if (!empty($fixture->table)) { $tableName = $fixture->table; } else { $tableName = (string) Inflector::underscore(str_replace_last('Fixture', '', $fixtureClassName)); } list($fields, $constraints, $indexes, $options) = $this->_prepareSchemaProperties($fixture); $schema = new TableSchema($tableName, $fields); foreach ($constraints as $name => $attrs) { $schema->addConstraint($name, $attrs); } foreach ($indexes as $name => $attrs) { $schema->addIndex($name, $attrs); } if (!empty($options)) { $schema->options($options); } return $schema; }
/** * Test the options method. * * @return void */ public function testOptions() { $table = new Table('articles'); $options = ['engine' => 'InnoDB']; $return = $table->options($options); $this->assertInstanceOf('Cake\\Database\\Schema\\Table', $return); $this->assertEquals($options, $table->options()); }