In my case I discovered some “0000–00–00” dates in my mysql database so I want to run this command when a connection is established to prevent it:


SET sql_mode="STRICT_ALL_TABLE,NO_ZERO_DATE,NO_ZERO_IN_DATE";

I could probably do it with PDO::MYSQL_ATTR_INIT_COMMAND option but there is maybe some better way in Doctrine?

Sure enough this works. Is it the best approach?


kdyby.doctrine:
    driverOptions:
        # Prevent dates like 0000-00-00.
        # 1002 is \PDO::MYSQL_ATTR_INIT_COMMAND
        1002: "SET sql_mode='STRICT_ALL_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE';"

Beware that you need to use driverOptions, not just options. You don't need to bother with \PDO::ATTR_ERRMODE because Doctrine sets that to exception by itself.

EDIT: It's probably a bit better to use SET sql_mode='TRADITIONAL'; to enable some additional strict checks.


You must first log in to participate in this discussion