IN Symfony, when i used the following query with DATE function in the mysql i get a error
SELECT employer.companyName AS employerName, jobs.jobId, jobs.
DQL
is only aware of few standard sql functions (coalesce
for example). To be able to use your custom function you need to register it and tell doctrine
how to translate it into raw sql. Follow these guides:
Symfony Doc
Doctrine Doc
And check my answer here
Or if you don't want add a new dependency, you can use code like this:
$dateTime = new \DateTime();
$qb
->andWhere('jobs.endDate BETWEEN :dateMin AND :dateMax')
->setParameters(
[
'dateMin' => $dateTime->format('Y-m-d 00:00:00'),
'dateMax' => $dateTime->format('Y-m-d 23:59:59'),
]
);
In addition to the accepted answer there are a tonne of pre-built custom functions available at https://github.com/beberlei/DoctrineExtensions .
These can be then registered in your config like
doctrine:
orm:
dql:
string_functions:
DATE: DoctrineExtensions\Query\Mysql\Date
and can then be used in your DQL (as in your query) like
DATE(jobs.endDate) AS endDate