本文整理汇总了PHP中Cake\Database\Schema\Table类的典型用法代码示例。如果您正苦于以下问题:PHP Table类的具体用法?PHP Table怎么用?PHP Table使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Table类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _initializeSchema
/**
* @param \Cake\Database\Schema\Table $table Table schema
* @return \Cake\Database\Schema\Table
*/
protected function _initializeSchema(Schema $table)
{
$table->columnType('payload', 'serialize');
$table->columnType('options', 'serialize');
$table->columnType('history', 'json');
return parent::_initializeSchema($table);
}
示例2: _ensureTable
protected function _ensureTable($archiveTable)
{
try {
$archiveTable->schema();
} catch (Exception $e) {
$djSchema = TableRegistry::get('DelayedJobs.DelayedJobs')->schema();
$djColumns = $djSchema->columns();
$columns = [];
foreach ($djColumns as $djColumn) {
$columns[$djColumn] = $djSchema->column($djColumn);
}
$columns['payload']['type'] = 'binary';
$columns['options']['type'] = 'binary';
$archiveTableSchema = new Table($archiveTable->table(), $columns);
$archiveTableSchema->addConstraint('primary', $djSchema->constraint('primary'));
$createSql = $archiveTableSchema->createSql($archiveTable->connection());
foreach ($createSql as $createSqlQuery) {
$archiveTable->connection()->query($createSqlQuery);
}
}
}
示例3: _initializeSchema
/**
* Initialize schema method
*
* @param \Cake\Database\Schema\Table $schema The schema of the Table.
*
* @return \Cake\Database\Schema\Table
*/
protected function _initializeSchema(Schema $schema)
{
$schema->columnType('secret', 'encryptedsecurity');
$schema->columnType('username', 'encryptedsecurity');
$schema->columnType('session', 'encryptedsecurity');
$schema->columnType('recovery_code', 'encryptedsecurity');
return $schema;
}
示例4: fieldValidation
/**
* Does individual field validation handling.
*
* @param \Cake\Database\Schema\Table $schema The table schema for the current field.
* @param string $fieldName Name of field to be validated.
* @param array $metaData metadata for field
* @param string $primaryKey The primary key field
* @return array Array of validation for the field.
*/
public function fieldValidation($schema, $fieldName, array $metaData, $primaryKey)
{
$ignoreFields = ['created', 'modified', 'updated'];
if (in_array($fieldName, $ignoreFields)) {
return false;
}
$rule = false;
if ($fieldName === 'email') {
$rule = 'email';
} elseif ($metaData['type'] === 'uuid') {
$rule = 'uuid';
} elseif ($metaData['type'] === 'integer') {
$rule = 'numeric';
} elseif ($metaData['type'] === 'float') {
$rule = 'numeric';
} elseif ($metaData['type'] === 'decimal') {
$rule = 'decimal';
} elseif ($metaData['type'] === 'boolean') {
$rule = 'boolean';
} elseif ($metaData['type'] === 'date') {
$rule = 'date';
} elseif ($metaData['type'] === 'time') {
$rule = 'time';
} elseif ($metaData['type'] === 'datetime') {
$rule = 'datetime';
} elseif ($metaData['type'] === 'inet') {
$rule = 'ip';
}
$allowEmpty = false;
if (in_array($fieldName, $primaryKey)) {
$allowEmpty = 'create';
} elseif ($metaData['null'] === true) {
$allowEmpty = true;
}
$validation = ['valid' => ['rule' => $rule, 'allowEmpty' => $allowEmpty]];
foreach ($schema->constraints() as $constraint) {
$constraint = $schema->constraint($constraint);
if (!in_array($fieldName, $constraint['columns']) || count($constraint['columns']) > 1) {
continue;
}
if ($constraint['type'] === SchemaTable::CONSTRAINT_UNIQUE) {
$validation['unique'] = ['rule' => 'validateUnique', 'provider' => 'table'];
}
}
return $validation;
}
示例5: testResetDbIfTableExists
/**
* Test that if a table already exists in the test database, it will dropped
* before being recreated
*
* @return void
*/
public function testResetDbIfTableExists()
{
$db = ConnectionManager::get('test');
$restore = $db->logQueries();
$db->logQueries(true);
$this->manager->setDebug(true);
$buffer = new ConsoleOutput();
Log::config('testQueryLogger', ['className' => 'Console', 'stream' => $buffer]);
$table = new Table('articles', ['id' => ['type' => 'integer', 'unsigned' => true], 'title' => ['type' => 'string', 'length' => 255]]);
$table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$sql = $table->createSql($db);
foreach ($sql as $stmt) {
$db->execute($stmt);
}
$test = $this->getMockBuilder('Cake\\TestSuite\\TestCase')->getMock();
$test->fixtures = ['core.articles'];
$this->manager->fixturize($test);
$this->manager->load($test);
$db->logQueries($restore);
$this->assertContains('DROP TABLE', implode('', $buffer->messages()));
}
示例6: indexSql
/**
* {@inheritDoc}
*/
public function indexSql(Table $table, $name)
{
$data = $table->index($name);
if ($data['type'] === Table::INDEX_INDEX) {
$out = 'KEY ';
}
if ($data['type'] === Table::INDEX_FULLTEXT) {
$out = 'FULLTEXT KEY ';
}
$out .= $this->_driver->quoteIdentifier($name);
return $this->_keySql($out, $data);
}
示例7: schema
/**
* Returns the schema table object describing this table's properties.
*
* If an \Cake\Database\Schema\Table is passed, it will be used for this table
* instead of the default one.
*
* If an array is passed, a new \Cake\Database\Schema\Table will be constructed
* out of it and used as the schema for this table.
*
* @param array|\Cake\Database\Schema\Table|null $schema New schema to be used for this table
* @return \Cake\Database\Schema\Table
*/
public function schema($schema = null)
{
if ($schema === null) {
if ($this->_schema === null) {
$this->_schema = $this->_initializeSchema($this->connection()->schemaCollection()->describe($this->table()));
}
return $this->_schema;
}
if (is_array($schema)) {
$constraints = [];
if (isset($schema['_constraints'])) {
$constraints = $schema['_constraints'];
unset($schema['_constraints']);
}
$schema = new Schema($this->table(), $schema);
foreach ($constraints as $name => $value) {
$schema->addConstraint($name, $value);
}
}
return $this->_schema = $schema;
}
示例8: testTruncateSql
/**
* Test truncateSql()
*
* @return void
*/
public function testTruncateSql()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMockBuilder('Cake\\Database\\Connection')->disableOriginalConstructor()->getMock();
$connection->expects($this->any())->method('driver')->will($this->returnValue($driver));
$table = new Table('articles');
$result = $table->truncateSql($connection);
$this->assertCount(1, $result);
$this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
}
示例9: testTruncateSqlNoSequences
/**
* Test truncateSql() with no sequences
*
* @return void
*/
public function testTruncateSqlNoSequences()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\\Database\\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')->will($this->returnValue($driver));
$statement = $this->getMock('\\PDOStatement', ['execute', 'rowCount', 'closeCursor', 'fetch']);
$driver->connection()->expects($this->once())->method('prepare')->with('SELECT 1 FROM sqlite_master WHERE name = "sqlite_sequence"')->will($this->returnValue($statement));
$statement->expects($this->once())->method('fetch')->will($this->returnValue(false));
$table = new Table('articles');
$result = $table->truncateSql($connection);
$this->assertCount(1, $result);
$this->assertEquals('DELETE FROM "articles"', $result[0]);
}
示例10: columnSql
/**
* {@inheritDoc}
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' LONGBLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)'];
$specialMap = ['string' => true];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if (isset($specialMap[$data['type']])) {
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
}
}
$hasLength = ['integer', 'string'];
if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
$out .= '(' . (int) $data['length'] . ')';
}
$hasPrecision = ['float', 'decimal'];
if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
$hasUnsigned = ['float', 'decimal', 'integer', 'biginteger'];
if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
$out .= ' UNSIGNED';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
$addAutoIncrement = [$name] == (array) $table->primaryKey() && !$table->hasAutoIncrement();
if (in_array($data['type'], ['integer', 'biginteger']) && ($data['autoIncrement'] === true || $addAutoIncrement)) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) {
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
unset($data['default']);
}
if (isset($data['default']) && in_array($data['type'], ['timestamp', 'datetime']) && strtolower($data['default']) === 'current_timestamp') {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
unset($data['default']);
}
if (isset($data['comment']) && $data['comment'] !== '') {
$out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
}
return $out;
}
示例11: _convertConstraint
/**
* Add/update a constraint into the schema object.
*
* @param \Cake\Database\Schema\Table $table The table to update.
* @param string $name The index name.
* @param string $type The index type.
* @param array $row The metadata record to update with.
* @return void
*/
protected function _convertConstraint($table, $name, $type, $row)
{
$constraint = $table->constraint($name);
if (!$constraint) {
$constraint = ['type' => $type, 'columns' => []];
}
$constraint['columns'][] = $row['attname'];
$table->addConstraint($name, $constraint);
}
示例12: column
/**
* Returns an array of column data for a single column
*
* @param \Cake\Database\Schema\Table $tableSchema Name of the table to retrieve columns for
* @param string $column A column to retrieve data for
* @return array
*/
public function column($tableSchema, $column)
{
return ['columnType' => $tableSchema->columnType($column), 'options' => $this->attributes($tableSchema->name(), $column)];
}
示例13: Table
<?php
/**
* Queued Tasks schema file
*
* @author David Yell <neon1024@gmail.com>
* @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']);
示例14: _initializeSchema
/**
* _initializeSchema
*/
protected function _initializeSchema(\Cake\Database\Schema\Table $table)
{
$table->columnType('image', 'proffer.file');
return $table;
}
示例15: testInitNoImportNoFields
/**
* test schema reflection without $import or $fields will reflect the schema
*
* @return void
*/
public function testInitNoImportNoFields()
{
$db = ConnectionManager::get('test');
$collection = $db->schemaCollection();
if (!in_array('letters', $collection->listTables())) {
$table = new Table('letters', ['id' => ['type' => 'integer'], 'letter' => ['type' => 'string', 'length' => 1]]);
$table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$sql = $table->createSql($db);
foreach ($sql as $stmt) {
$db->execute($stmt);
}
}
$fixture = new LettersFixture();
$fixture->init();
$this->assertEquals(['id', 'letter'], $fixture->schema()->columns());
$db = $this->getMockBuilder('Cake\\Database\\Connection')->setMethods(['prepare', 'execute'])->disableOriginalConstructor()->getMock();
$db->expects($this->never())->method('prepare');
$db->expects($this->never())->method('execute');
$this->assertTrue($fixture->create($db));
$this->assertTrue($fixture->drop($db));
}