This library provide set of cross database doctrine DQL functions. Supported databases are MySQL and PostgreSQL. Available functions:
DATE(expr)
- Extract the date part of a date or datetime expressionTIME(expr)
- Extract the time portion of the expression passedTIMESTAMP(expr)
- Convert expression to TIMESTAMPTIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2)
- Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. The unit should be one of the following values: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.CONVERT_TZ(expr, from_tz, to_tz)
- Converts a datetime value expr from the time zone given by from_tz to the time zone given by to_tz and returns the resulting datetime valueDAY(expr)
- Return the day of the month (0-31)DAYOFWEEK(expr)
- Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.DAYOFMONTH(expr)
- Returns the day of the month for date, in the range 1 to 31, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero day part.DAYOFYEAR(expr)
- Return the day of the year (1-366)HOUR(expr)
- Return the hour from the date passedMD5(expr)
- Calculate MD5 checksumMINUTE(expr)
- Return the minute from the date passedMONTH(expr)
- Return the month from the date passedQUARTER(expr)
- Return the quarter from the date passedSECOND(expr)
- Return the second from the date passedWEEK(expr)
- The number of the week of the year that the day is in. By definition (ISO 8601), weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year.YEAR(expr)
- Return the year from the date passedPOW(expr, power)
- Return the argument raised to the specified powerROUND(value, precision)
- Return the value formated with the precision specifiedSIGN(expr)
- Return the sign of the argumentCAST(expr as type)
- Takes an expression of any type and produces a result value of a specified type. Supported types are: "char, string, text, date, datetime, time, int, integer, decimal, boolean"CONCAT_WS
- Concatenate all but the first argument with separators. The first argument is used as the separator string.GROUP_CONCAT
- Return a concatenated stringREPLACE(subject,from,to)
- Replaces all occurrances of a string "from" with "to" within a string "subject"DATE_FORMAT(date,format)
- Formats the date value according to the format string. The following specifiers may be used in the format string. The % character is required before format specifier characters.
Specifier | Description |
---|---|
%a | Abbreviated weekday name (Sun..Sat) |
%b | Abbreviated month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%d | Day of the month, numeric (00..31) |
%e | Day of the month, numeric (0..31) |
%f | Microseconds (000000..999999) |
%H | Hour (00..23) |
%h | Hour (01..12) |
%I | Hour (01..12) |
%i | Minutes, numeric (00..59) |
%j | Day of year (001..366) |
%k | Hour (0..23) |
%l | Hour (1..12) |
%M | Month name (January..December) |
%m | Month, numeric (00..12) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
%S | Seconds (00..59) |
%s | Seconds (00..59) |
%T | Time, 24-hour (hh:mm:ss) |
%W | Weekday name (Sunday..Saturday) |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal % character |
GROUP_CONCAT full syntax:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
Add the following dependency to your composer.json
{
"require": {
"odinsey/doctrine-extensions": "dev-master"
}
}
Doctrine2 Documentation: "DQL User Defined Functions"
<?php
$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction('group_concat', 'Odinsey\ORM\Query\AST\Functions\String\GroupConcat');
$config->addCustomNumericFunction('hour', 'Odinsey\ORM\Query\AST\Functions\SimpleFunction');
$config->addCustomDatetimeFunction('date', 'Odinsey\ORM\Query\AST\Functions\SimpleFunction');
$em = EntityManager::create($dbParams, $config);
In Symfony2 you can register functions in config.yml
doctrine:
orm:
dql:
datetime_functions:
date: Odinsey\ORM\Query\AST\Functions\SimpleFunction
time: Odinsey\ORM\Query\AST\Functions\SimpleFunction
timestamp: Odinsey\ORM\Query\AST\Functions\SimpleFunction
convert_tz: Odinsey\ORM\Query\AST\Functions\DateTime\ConvertTz
numeric_functions:
timestampdiff: Odinsey\ORM\Query\AST\Functions\Numeric\TimestampDiff
dayofyear: Odinsey\ORM\Query\AST\Functions\SimpleFunction
dayofmonth: Odinsey\ORM\Query\AST\Functions\SimpleFunction
dayofweek: Odinsey\ORM\Query\AST\Functions\SimpleFunction
week: Odinsey\ORM\Query\AST\Functions\SimpleFunction
day: Odinsey\ORM\Query\AST\Functions\SimpleFunction
hour: Odinsey\ORM\Query\AST\Functions\SimpleFunction
minute: Odinsey\ORM\Query\AST\Functions\SimpleFunction
month: Odinsey\ORM\Query\AST\Functions\SimpleFunction
quarter: Odinsey\ORM\Query\AST\Functions\SimpleFunction
second: Odinsey\ORM\Query\AST\Functions\SimpleFunction
year: Odinsey\ORM\Query\AST\Functions\SimpleFunction
sign: Odinsey\ORM\Query\AST\Functions\Numeric\Sign
pow: Odinsey\ORM\Query\AST\Functions\Numeric\Pow
round: Odinsey\ORM\Query\AST\Functions\Numeric\Round
string_functions:
md5: Odinsey\ORM\Query\AST\Functions\SimpleFunction
group_concat: Odinsey\ORM\Query\AST\Functions\String\GroupConcat
concat_ws: Odinsey\ORM\Query\AST\Functions\String\ConcatWs
cast: Odinsey\ORM\Query\AST\Functions\Cast
replace: Odinsey\ORM\Query\AST\Functions\String\Replace
date_format: Odinsey\ORM\Query\AST\Functions\String\DateFormat
If you are using an ORM service provider make sure that you are adding the custom function to the configuration
for palmasev/DoctrineORMServiceProvider
$config = $app['doctrine_orm.configuration'];
$config->addCustomDateTimeFunction( 'year', 'Odinsey\ORM\Query\AST\Functions\SimpleFunction' );
$config->addCustomDateTimeFunction( 'month', 'Odinsey\ORM\Query\AST\Functions\SimpleFunction' );
for dflydev/dflydev-doctrine-orm-service-provider
$app->register( new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider, [
...
'orm.custom.functions.string' => [
'md5' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'cast' => 'Odinsey\ORM\Query\AST\Functions\Cast',
'group_concat' => 'Odinsey\ORM\Query\AST\Functions\String\GroupConcat',
'concat_ws' => 'Odinsey\ORM\Query\AST\Functions\String\ConcatWs',
'replace' => 'Odinsey\ORM\Query\AST\Functions\String\Replace',
'date_format' => 'Odinsey\ORM\Query\AST\Functions\String\DateFormat'
],
'orm.custom.functions.datetime' => [
'date' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'time' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'timestamp' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'convert_tz' => 'Odinsey\ORM\Query\AST\Functions\DateTime\ConvertTz'
],
'orm.custom.functions.numeric' => [
'timestampdiff' => 'Odinsey\ORM\Query\AST\Functions\Numeric\TimestampDiff',
'dayofyear' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'dayofweek' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'week' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'day' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'hour' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'minute' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'month' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'quarter' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'second' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'year' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'sign' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Sign',
'pow' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Pow',
'round' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Round',
]
]);
In Zend Framework 2 you can register functions in config/autoload/doctrine.global.php
return [
'doctrine' => [
'configuration' => [
'orm_default' => [
'datetime_functions' => [
'date' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'time' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'timestamp' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'convert_tz' => 'Odinsey\ORM\Query\AST\Functions\DateTime\ConvertTz',
],
'numeric_functions' => [
'timestampdiff' => 'Odinsey\ORM\Query\AST\Functions\Numeric\TimestampDiff',
'dayofyear' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'dayofmonth' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'dayofweek' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'week' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'day' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'hour' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'minute' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'month' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'quarter' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'second' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'year' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'sign' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Sign',
'pow' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Pow',
'round' => 'Odinsey\ORM\Query\AST\Functions\Numeric\Round',
],
'string_functions' => [
'md5' => 'Odinsey\ORM\Query\AST\Functions\SimpleFunction',
'group_concat' => 'Odinsey\ORM\Query\AST\Functions\String\GroupConcat',
'cast' => 'Odinsey\ORM\Query\AST\Functions\Cast',
'concat_ws' => 'Odinsey\ORM\Query\AST\Functions\String\ConcatWs',
'replace' => 'Odinsey\ORM\Query\AST\Functions\String\Replace',
'date_format' => 'Odinsey\ORM\Query\AST\Functions\String\DateFormat'
]
]
]
]
];
Most of functions, that require only one ArithmeticPrimary argument may be parsed with Odinsey\ORM\Query\AST\Functions\SimpleFunction
.
This class is responsible for parsing function definition and saving parsed data to parameters. It is extended from
Odinsey\ORM\Query\AST\Functions\AbstractPlatformAwareFunctionNode
. This layer work with DQL function parsing.
SQL generation is responsibility of platform specific functions, that extends PlatformFunctionNode
.
AbstractPlatformAwareFunctionNode
creates appropriate instance of platform function based on current connection Database Platform instance name and DQL function name.
Platform function classes naming rule is:
Odinsey\ORM\Query\AST\Platform\Functions\$platformName\$functionName
To add support of new platform you just need to create new folder Odinsey\ORM\Query\AST\Platform\Functions\$platformName
and implement required function there according to naming rules
In case when your function is function with only one ArithmeticPrimary argument you may not create DQL function parser
and use Odinsey\ORM\Query\AST\Functions\SimpleFunction
for this.
Then only platform specific SQL implementation of your function is required.
In case when your are implementing more complex function, like GROUP_CONCAT both DQL parser and SQL implementations are required.
If you want to add new function to this library feel free to fork it and create pull request with your implementation. Please, remember to update documentation with your new functions. All new functions MUST be implemented for all platforms.
This library also consist additional field types:
MoneyType
PercentType
ObjectType
ArrayType
ObjectType
and ArrayType
use base64 encoded string to store values in Db instead of storing serialized strings.
For backward compatibility values that are already stored in Db will be unserialized without base64 encoding. New values
will be base64 encoded before storing in Db and base64 decoded before unserialization.