Symfony2 Console Table Helper を試してみます

実装コード

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConsoleTableHelperCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('sample:table-helper');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getManager();
        $repo = $em->getRepository('XxxxBundle:Mercury');

        $dql = <<<DQL
SELECT
  m.mercury, COUNT(m.mercury) cnt
FROM
  XxxxBundle:Mercury m
GROUP BY
  m.mercury
DQL;

        $rows = $em->createQuery($dql)->getResult();

        $table = $this->getHelper('table');
        $table->setHeaders(array('Mercury', 'Count'));
        $table->setRows($rows);
        $table->render($output);
    }
}

実行結果

$ php app/console sample:table-helper
+---------+-------+
| Mercury | Count |
+---------+-------+
| 15.00   | 89    |
| 16.00   | 79    |
| 17.00   | 84    |
...
| 33.00   | 68    |
| 34.00   | 93    |
| 35.00   | 80    |
+---------+-------+

これは助かりますね!