Skip to content

Instantly share code, notes, and snippets.

@andrerom
Created August 14, 2013 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrerom/6232275 to your computer and use it in GitHub Desktop.
Save andrerom/6232275 to your computer and use it in GitHub Desktop.
For dumping a database to a "fixture" php file, as used by integration tests in eZ Publish 5.x
#!/usr/bin/env php
<?php
require 'ezc/Base/base.php';
spl_autoload_register( 'ezcBase::autoload' );
$db = ezcDbFactory::create( 'mysql://user:password@server/databse_name' );
// Get all tables
$tables = array();
$result = $db->query( 'SHOW TABLES' );
while ( $row = $result->fetch( PDO::FETCH_COLUMN ) )
{
// Filtering out some tables
if ( strpos( $row, 'ezx_' ) !== 0 )
$tables[] = $row;
}
// get data
$fixture = array();
foreach ( $tables as $table )
{
$result = $db->query( 'SELECT * FROM ' . $table );
$fixture[$table] = array();
while ( $row = $result->fetch( PDO::FETCH_ASSOC ) )
{
$fixture[$table][] = $row;
}
}
echo "<?php\n\nreturn ", str_replace( array( "\n " ,"\n ", "\n ", " => \n" ) , array( "\n", "\n ", "\n ", " =>\n" ), var_export( $fixture, true ) ), ";\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment