Skip to content

Instantly share code, notes, and snippets.

@alongosz
Last active June 22, 2017 15:11
Show Gist options
  • Save alongosz/a8bfc51163141767543455fc0f6aa520 to your computer and use it in GitHub Desktop.
Save alongosz/a8bfc51163141767543455fc0f6aa520 to your computer and use it in GitHub Desktop.
Display database table contents using Symfony Table helper and Doctrine\DBAL\Connection
/**
* @param \Doctrine\DBAL\Connection $connection
* @param string $table db table name
* @param array $predicates
*/
private function displayDbTable(\Doctrine\DBAL\Connection $connection, $table, array $predicates = [])
{
$query = $connection->createQueryBuilder();
$query
->select('*')
->from($table)
->where('1=1')
;
$i = 0;
foreach ($predicates as $predicate => $value) {
$query->andWhere($predicate);
$query->setParameter($i++, $value);
}
$stmt = $query->execute();
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if (empty($data)) {
echo "$table is empty", PHP_EOL;
return;
}
$table = new \Symfony\Component\Console\Helper\Table(
new \Symfony\Component\Console\Output\ConsoleOutput()
);
$table
->setHeaders(array_keys($data[0]))
->setRows($data)
;
$table->render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment