How to ORDER BY DateTime in Doctrine 2?

Database

You should check the table structure in the database:

What type of column is date_start?
It should be DATETIME (in MySQL, other vendors may vary).
But I suspect it is some form of string, like VARCHAR (or CHAR, TEXT, etc).

When this is the case, the result is ordered, but not in the way you expect it. This is because different types are ordered differently by the database.

CLI tool

Doctrine comes with a console tool that's able to validate your mappings and check if the database is consistent with them: doctrine orm:validate-schema.

If it reports inconsistencies with the database, use doctrine orm:schema-tool:update --dump-sql to have it display the queries it would perform to update the database (it will assume the mappings are the source of truth).


your Query would be like :

    $qb->select( 'e' )
        ->from( 'Entity\Event',  'e' )
        ->orderBy('e.dateStart', 'ASC');
        ->setFirstResult( $offset )
        ->setMaxResults(20);

you have to respect the order of query builder parameters i hope that will help.