public function testCompositeForeignKeys() { $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition())); $fkeys = $this->manager->listTableForeignKeys('dummy'); $this->assertEquals(1, count($fkeys), "Table has to have one foreign key."); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $fkeys[0]); $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns())); $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns())); }
/** * @param string $table * @param string $column * @return string * @throws \Exception */ private function getForeignKeyConstraint($table, $column) { $keys = $this->schemaManager->listTableForeignKeys($table); foreach ($keys as $key) { if (in_array($column, $key->getLocalColumns(), false)) { return $key->getName(); } } throw new \Exception('Foreign key constraint not found.'); }
public function __construct(\Doctrine\DBAL\Schema\AbstractSchemaManager $schema_manager) { $this->schema_manager = $schema_manager; foreach ($schema_manager->listTables() as $table) { $this->tables[$table->getName()] = new TableInformation($this, $table); } foreach ($this->tables as $table) { $this->findRelationships($table); } }
public function onSuccess(Form $form) { if (!Callback::create($this->checkConnection)->invoke() || !$this->schemaManager->tablesExist('users')) { return; } $presenter = $form->presenter; $logEntity = new LogEntity($this->user instanceof UserEntity ? $this->user : NULL, 'Venne\\Forms\\Form', NULL, LogEntity::ACTION_OTHER); $logEntity->setType($presenter->link('this')); $logEntity->setMessage('Configuration has been updated'); $this->logRepository->save($logEntity); }
/** * Create array of all the fields for a table * * @param string $table Table Name * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @param string $database * * @return array|bool */ public function generate($table, $schema, $database) { $this->database = $database; $columns = $schema->listTableColumns($table); if (empty($columns)) { return false; } $indexParser = new IndexParser($table, $schema); $fields = $this->setEnum($this->getFields($columns, $indexParser), $table); $indexes = $this->getMultiFieldIndexes($indexParser); return array_merge($fields, $indexes); }
/** * Create array of all the fields for a table * * @param string $table Table Name * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @param string $database * @param bool $ignoreIndexNames * * @return array */ public function generate($table, $schema, $database, $ignoreIndexNames) { $this->database = $database; $columns = $schema->listTableColumns($table); if (empty($columns)) { return []; } $indexGenerator = new IndexGenerator($table, $schema, $ignoreIndexNames); $fields = $this->setEnum($this->getFields($columns, $indexGenerator), $table); $fields = $this->getMultiFieldIndexes($fields, $indexGenerator); return $fields; }
public function __construct() { $this->schema = DB::getDoctrineSchemaManager(); $this->schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $this->tables = $this->schema->listTables(); $this->relationships = []; //first create empty ruleset for each table foreach ($this->tables as $table) { $this->relationships[$table->getName()] = ['hasMany' => [], 'hasOne' => [], 'belongsTo' => [], 'belongsToMany' => []]; } // get all relationships into $this->relationships variable $this->getAllRelationships(); }
/** * @param $file * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm * @return bool */ public static function saveSchemaToFile($file, $sm) { $xml = new SimpleXMLElement('<database/>'); $xml->addChild('name', OC_Config::getValue("dbname", "owncloud")); $xml->addChild('create', 'true'); $xml->addChild('overwrite', 'false'); $xml->addChild('charset', 'utf8'); foreach ($sm->listTables() as $table) { self::saveTable($table, $xml->addChild('table')); } file_put_contents($file, $xml->asXML()); return true; }
/** * Get array of foreign keys * * @param string $table Table Name * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * * @return array */ public function generate($table, $schema) { $this->table = $table; $fields = []; $foreignKeys = $schema->listTableForeignKeys($table); if (empty($foreignKeys)) { return array(); } foreach ($foreignKeys as $foreignKey) { $fields[] = ['name' => $this->getName($foreignKey), 'field' => $foreignKey->getLocalColumns()[0], 'references' => $foreignKey->getForeignColumns()[0], 'on' => $foreignKey->getForeignTableName(), 'onUpdate' => $foreignKey->hasOption('onUpdate') ? $foreignKey->getOption('onUpdate') : 'RESTRICT', 'onDelete' => $foreignKey->hasOption('onDelete') ? $foreignKey->getOption('onDelete') : 'RESTRICT']; } return $fields; }
/** * Create array of all the fields for a table * * @param string $table Table Name * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @param string $database * @param bool $ignoreIndexNames * * @return array|bool */ public function generate($table, $schema, $database, $ignoreIndexNames) { $this->defaultConnection = DB::getDefaultConnection(); $this->database = $database; $columns = $schema->listTableColumns($table); if (empty($columns)) { return false; } $indexGenerator = new IndexGenerator($table, $schema, $ignoreIndexNames); $fields = $this->setEnum($this->getFields($columns, $indexGenerator), $table); $fields = $this->getTableProperty($fields, $table); $indexes = $this->getMultiFieldIndexes($indexGenerator); return array_merge($fields, $indexes); }
/** * constructor * @param string $table_name * @param Connection $conn */ public function __construct($table_name, \Doctrine\DBAL\Connection $conn) { $this->conn = $conn; $this->table_name = $table_name; $this->quoted_table_name = $this->conn->quoteIdentifier($this->table_name); $this->sm = $this->conn->getSchemaManager(); if (!$this->sm->tablesExist([$table_name])) { throw Schema\SchemaException::tableDoesNotExist($table_name); } foreach ($this->sm->listTableColumns($this->table_name) as $colum) { $this->columns[$colum->getName()] = $colum; $this->column_types[$colum->getName()] = $colum->getType()->getName(); } }
/** * @param string $table Table Name * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @param bool $ignoreIndexNames */ public function __construct($table, $schema) { $this->indexes = array(); $this->multiFieldIndexes = array(); $indexes = $schema->listTableIndexes($table); foreach ($indexes as $index) { $indexArray = $this->indexToArray($table, $index); if (count($indexArray['columns']) == 1) { $columnName = $indexArray['columns'][0]; $this->indexes[$columnName] = (object) $indexArray; } else { $this->multiFieldIndexes[] = (object) $indexArray; } } }
/** * Migrates the database. * * @return Schema */ public function migrate() { $diff = Comparator::compareSchemas($this->manager->createSchema(), $this->schema); foreach ($diff->toSaveSql($this->connection->getDatabasePlatform()) as $query) { $this->connection->executeQuery($query); } }
/** * Execute changes */ public function executeChanges() { $platform = $this->sm->getDatabasePlatform(); $sql = []; if (count($this->changedIndexes)) { foreach ($this->changedIndexes as $index) { $sql[] = $platform->getDropIndexSQL($index); $sql[] = $platform->getCreateIndexSQL($index, $this->table); } } if (count($this->dropIndexes)) { foreach ($this->dropIndexes as $index) { $sql[] = $platform->getDropIndexSQL($index); } } if (count($this->addedIndexes)) { foreach ($this->addedIndexes as $index) { $sql[] = $platform->getCreateIndexSQL($index, $this->table); } } if (count($sql)) { foreach ($sql as $query) { $this->db->executeUpdate($query); } $this->changedIndexes = []; $this->dropIndexes = []; $this->addedIndexes = []; } }
/** * Gets array \Doctrine\DBAL\Schema\View * * @return array */ public function get_views() { /** * get views */ $views = $this->sm->listViews(); return $views; }
/** * {@inheritdoc} */ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null) { $indexes = array(); foreach ($tableIndexes as $k) { $k['primary'] = (bool) $k['primary']; $indexes[] = $k; } return parent::_getPortableTableIndexesList($indexes, $tableName); }
private function tableExist() { try { $name = $this->em->getClassMetadata('RenderBundle:Queue')->getTableName(); return $this->sm->tablesExist($name); } catch (\Exception $e) { return false; } }
public function testAlterTableThrowsExceptionForChangedSpatialType() { $this->setExpectedException('\\RuntimeException', 'The geometry_type of a spatial column cannot be changed (Requested changing type from "POINT" to "LINESTRING" for column "point_2d" in table "points")'); $table = $this->sm->listTableDetails('points'); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('points'); $tableDiff->fromTable = $table; $tableDiff->changedColumns[] = new ColumnDiff('point_2d', new \Doctrine\DBAL\Schema\Column('point_2d', Type::getType('geometry'), array('customSchemaOptions' => array('geometry_type' => 'LINESTRING'))), array('geometry_type'), $table->getColumn('point_2d')); $this->sm->alterTable($tableDiff); }
protected function getTestTable($name, $options=array()) { $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), false, $options); $table->setSchemaConfig($this->_sm->createSchemaConfig()); $table->addColumn('id', 'integer', array('notnull' => true)); $table->setPrimaryKey(array('id')); $table->addColumn('test', 'string', array('length' => 255)); $table->addColumn('foreign_key_test', 'integer'); return $table; }
/** * Determine if a table exists. * * @param string $table * @param bool $throwException * * @return bool * * @throws SchemaException */ public function checkTableExists($table, $throwException = false) { if ($this->sm->tablesExist($this->prefix . $table)) { if ($throwException) { throw new SchemaException($this->prefix . "{$table} already exists"); } return true; } return false; }
public function checkForForeignKeySourceTable($columnName) { // @var Doctrine\DBAL\Schema\ForeignKeyConstraint[] $fks = $this->schema->listTableForeignKeys($this->table->getName()); foreach ($fks as $f) { if (in_array($columnName, $f->getColumns())) { return $f->getForeignTableName(); } } return ''; }
protected function getTestCompositeTable($name) { $table = new Table($name, array(), array(), array(), false, array()); $table->setSchemaConfig($this->_sm->createSchemaConfig()); $table->addColumn('id', 'integer', array('notnull' => true)); $table->addColumn('other_id', 'integer', array('notnull' => true)); $table->setPrimaryKey(array('id', 'other_id')); $table->addColumn('test', 'string', array('length' => 255)); $table->addColumn('test_other', 'string', array('length' => 255)); return $table; }
/** * Determine if a table exists * * @param $table * @param bool $throwException * @return bool */ public function checkTableExists($table, $throwException = false) { if (!$this->sm->tablesExist($table)) { if ($throwException) { throw new SchemaUpdateException("{$table} does not exist"); } else { return false; } } else { return true; } }
/** * @param AbstractSchemaManager $schemaM * @param Schema $schema * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function createTable($schemaM, $schema) { if ($schemaM->tablesExist('artists')) { $schemaM->dropTable('artists'); } if ($schemaM->tablesExist('user')) { $schemaM->dropTable('user'); } echo 'Tables :: '; $UTable = $schema->createTable('user'); $UTable->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]); $UTable->addColumn('name', 'string', ['length' => 60]); $UTable->setPrimaryKey(['id']); $myTable = $schema->createTable('artists'); $myTable->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]); $myTable->addColumn('user_id', 'integer', ['unsigned' => true]); $myTable->addColumn('name', 'string', ['length' => 60]); $myTable->setPrimaryKey(['id']); $myTable->addForeignKeyConstraint($UTable, array('user_id'), array('id'), array('onUpdate' => 'CASCADE')); $schemaM->createTable($UTable); $schemaM->createTable($myTable); }
public function testCreateYamlWithForeignKeyFromDatabase() { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Platform does not support foreign keys.'); } $tableB = new \Doctrine\DBAL\Schema\Table("dbdriver_bar"); $tableB->addColumn('id', 'integer'); $tableB->setPrimaryKey(array('id')); $this->_sm->dropAndCreateTable($tableB); $tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz"); $tableA->addColumn('id', 'integer'); $tableA->setPrimaryKey(array('id')); $tableA->addColumn('bar_id', 'integer'); $tableA->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id')); $this->_sm->dropAndCreateTable($tableA); $metadata = $this->extractClassMetadata("DbdriverBaz"); $this->assertArrayNotHasKey('bar', $metadata->fieldMappings); $this->assertArrayHasKey('id', $metadata->fieldMappings); $metadata->associationMappings = \array_change_key_case($metadata->associationMappings, \CASE_LOWER); $this->assertArrayHasKey('bar', $metadata->associationMappings); $this->assertType('Doctrine\\ORM\\Mapping\\OneToOneMapping', $metadata->associationMappings['bar']); }
/** * @param $name * @return null */ protected function getColumnInfo($name) { if (is_null($this->columnsInfo)) { $sql = $this->schemaManager->getDatabasePlatform()->getListTableColumnsSQL($this->table); $this->columnsInfo = DB::select($sql); } foreach ($this->columnsInfo as $column) { if ($column->Field === $name) { return $column; } } return null; }
public function testLoadMetadataWithForeignKeyFromDatabase() { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Platform does not support foreign keys.'); } $tableB = new \Doctrine\DBAL\Schema\Table("dbdriver_bar"); $tableB->addColumn('id', 'integer'); $tableB->setPrimaryKey(array('id')); $this->_sm->dropAndCreateTable($tableB); $tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz"); $tableA->addColumn('id', 'integer'); $tableA->setPrimaryKey(array('id')); $tableA->addColumn('bar_id', 'integer'); $tableA->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id')); $this->_sm->dropAndCreateTable($tableA); $metadatas = $this->extractClassMetadata(array("DbdriverBar", "DbdriverBaz")); $this->assertArrayHasKey('DbdriverBaz', $metadatas); $bazMetadata = $metadatas['DbdriverBaz']; $this->assertArrayNotHasKey('barId', $bazMetadata->fieldMappings, "The foreign Key field should not be inflected as 'barId' field, its an association."); $this->assertArrayHasKey('id', $bazMetadata->fieldMappings); $bazMetadata->associationMappings = \array_change_key_case($bazMetadata->associationMappings, \CASE_LOWER); $this->assertArrayHasKey('bar', $bazMetadata->associationMappings); $this->assertEquals(ClassMetadataInfo::MANY_TO_ONE, $bazMetadata->associationMappings['bar']['type']); }
public function testListForeignKeysComposite() { if (!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Does not support foreign key constraints.'); } $this->_sm->createTable($this->getTestTable('test_create_fk3')); $this->_sm->createTable($this->getTestCompositeTable('test_create_fk4')); $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('id', 'foreign_key_test'), 'test_create_fk4', array('id', 'other_id'), 'foreign_key_test_fk'); $this->_sm->createForeignKey($foreignKey, 'test_create_fk3'); $fkeys = $this->_sm->listTableForeignKeys('test_create_fk3'); $this->assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key."); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $fkeys[0]); $this->assertEquals(array('id', 'foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns())); $this->assertEquals(array('id', 'other_id'), array_map('strtolower', $fkeys[0]->getForeignColumns())); }
/** * return the the interesect on primary index and foreign key. * if we have 2 primary key that are also foreign key without a type field it should be a pivot table ! * * @param $table * @return array */ public function checkPivots($table) { $foreignKeys = $this->schema->listTableForeignKeys($table); $indexes = $this->schema->listTableIndexes($table); $toto = []; foreach ($foreignKeys as $key) { $toto[] = $key->getLocalColumns(); } $tata = []; foreach ($indexes as $key) { if ($key->isPrimary()) { $tata[] = $key->getColumns(); } } return array_intersect(array_flatten($toto), array_flatten($tata)); }
/** * {@inheritdoc} */ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null) { foreach ($tableIndexes as $k => $v) { $v = array_change_key_case($v, CASE_LOWER); if ($v['key_name'] == 'PRIMARY') { $v['primary'] = true; } else { $v['primary'] = false; } if (strpos($v['index_type'], 'FULLTEXT') !== false) { $v['flags'] = array('FULLTEXT'); } $tableIndexes[$k] = $v; } return parent::_getPortableTableIndexesList($tableIndexes, $tableName); }