Skip to content

Instantly share code, notes, and snippets.

@ptlis
Last active July 4, 2017 11:50
Show Gist options
  • Save ptlis/e97d16551f967b6e3cc7271d8ea2b001 to your computer and use it in GitHub Desktop.
Save ptlis/e97d16551f967b6e3cc7271d8ea2b001 to your computer and use it in GitHub Desktop.
'Hack' to work around Doctrine issues with mysql >= 5.7 and queries involving DISTINCT and ORDER BY clauses
services:
Doctrine.SetSQLMode:
class: AppBundle\Listeners\SetSqlModeListener
tags:
- { name: doctrine.event_listener, event: postConnect }
<?php
namespace AppBundle\Listeners;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
/**
* Work-around for issues with Doctrine & order by clauses in SELECT DISTINCT queries.
*
* @TODO: This should be removed when doctrine is updated, see the issue below!
*
* https://github.com/doctrine/doctrine2/issues/5622
*/
class SetSqlModeListener
{
/**
* @param \Doctrine\DBAL\Event\ConnectionEventArgs $args
*
* @return void
*/
public function postConnect(ConnectionEventArgs $args)
{
// Loosy-goosey SQL mode; we'll depend on Doctrine to ensure correctness
$args->getConnection()->executeUpdate('SET sql_mode="TRADITIONAL,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"');
}
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return [Events::postConnect];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment