Last active
June 22, 2017 15:11
-
-
Save alongosz/a8bfc51163141767543455fc0f6aa520 to your computer and use it in GitHub Desktop.
Display database table contents using Symfony Table helper and Doctrine\DBAL\Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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