Skip to content

Instantly share code, notes, and snippets.

@Langmans
Last active August 29, 2015 14:14
Show Gist options
  • Save Langmans/665457e93c821a88fd9b to your computer and use it in GitHub Desktop.
Save Langmans/665457e93c821a88fd9b to your computer and use it in GitHub Desktop.
dbal test
{
"require": {
"doctrine/dbal": "~2.5"
}
}
<?php
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\DBAL\Schema\Schema;
require __DIR__ . '/vendor/autoload.php';
$conn = DriverManager::getConnection(array('url' => 'mysql://root@localhost/timeline?charset=UTF8'));
$conn->getConfiguration()->setSQLLogger(new EchoSQLLogger());
$schema_manager = $conn->getSchemaManager();
$current_schema = $schema_manager->createSchema();
$new_schema = new Schema();
$user_table = $new_schema->createTable('user');
$user_table->addColumn('id', 'integer')
->setAutoincrement(true)
->setUnsigned(true);
$user_table->addColumn('username', 'string');
$user_table->addColumn('password', 'string');
$user_table->addColumn('created_on', 'datetime');
$user_table->addColumn('updated_on', 'datetime');
$user_table->setPrimaryKey(array('id'));
$user_table->addUniqueIndex(array('username'));
foreach ($current_schema->getMigrateToSql($new_schema, $conn->getDatabasePlatform()) as $sql) {
var_dump($sql);
$conn->exec($sql);
}
// test it!
$query_builder = $conn->createQueryBuilder();
var_dump($query_builder
->from('user')
->select('*')
->execute()->fetchAll());
/** executed when refreshed **/
SHOW FULL TABLES WHERE Table_type = 'BASE TABLE';
SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS Collation FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'timeline' AND TABLE_NAME = 'user';
SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ FROM information_schema.key_column_usage k /*!50116 INNER JOIN information_schema.referential_constraints c ON c.constraint_name = k.constraint_name AND c.table_name = 'user' */ WHERE k.table_name = 'user' AND k.table_schema = 'timeline' /*!50116 AND c.constraint_schema = 'timeline' */ AND k.`REFERENCED_COLUMN_NAME` is not NULL;
SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment FROM information_schema.STATISTICS WHERE TABLE_NAME = 'user' AND TABLE_SCHEMA = 'timeline';
SELECT * FROM user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment