This library provides you MySQL functions for Doctrine2.
At the moment are supported
- CONCAT_WS
- GEO
- DATE
- YEAR
- MONTH
- DAY
Feel free to fork and add other functions.
Add this in your composer.json
{
"require": {
"glauberkyves/mysql-functions-doctrine2": "1.*"
}
}
and then run
php composer.phar update
or
composer update
if you installed composer globally.
$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction('concat_ws', 'GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlRand');
$em = EntityManager::create($dbParams, $config);
You can of course pick just the functions you need.
If you install the library in a Symfony2 application, you can add this in your config.yml
# app/config/config.yml
doctrine:
orm:
# ...
entity_managers:
default:
# ...
dql:
string_functions:
concat_ws: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlConcatWs
month: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlMonth
numeric_functions:
geo: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlGeo
datetime_functions:
date: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlDate
day: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlDay
year: GlauberKyves\MysqlDoctrineFunctions\DQL\MysqlYear
You can now use the functions in your DQL Query
$query = 'SELECT CONCAT_WS('string', 'string') FROM ...';
$em->createQuery($query);
$query = 'SELECT GEO(-15.5656, -47.5656, t.latitude, t.longitude) FROM tb_address t ...';
$em->createQuery($query);