iakumai / doctrine-functions
Doctrine DQL Functions for Mysql
Installs: 2 840
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-12-21 15:49:47 UTC
README
This package contains some doctrine functions
String functions
INSTR(exp1, exp2)
- documentationFIELD(exp1, exp2, exp3, exp4...)
- documentation
DateTime functions
DATE_FORMAT(field, '%format%')
- documentationDATE(field)
- documentation
Math functions
RAND()
- documentation. Remember, you can not use parameters in this function.RANDP(12345)
- documentation. This is still RAND() MySQL function, but you must use a number parameter in it.
Installation
Just add the package to your composer.json
{ "require": { "iakumai/doctrine-functions": "dev-master" } }
Integration
1) Doctrine Only
According to the Doctrine documentation you can register the functions in this package this way.
<?php $config = new \Doctrine\ORM\Configuration(); $config->addCustomDatetimeFunction('instr', 'IAkumaI\DQL\Str\Instr'); ?>
2) Using Symfony 2
With symfony 2 you can register yout functions in the config.yml
file.
doctrine: orm: entity_managers: default: dql: datetime_functions: instr: IAkumaI\DQL\Str\Instr
Usage
Simple example, usage a DateFormat function:
<?php use Doctrine\ORM\EntityManager; // EntityManager $em->createQuery("SELECT DATE_FORMAT(e.date, '%d.%m.%Y') as df FROM YourBundle:Ent e"); ?>
This way you can use DQL function in ORDER statement. For example, order by RAND():
<?php use Doctrine\ORM\EntityManager; // EntityManager $em->createQuery("SELECT e, RAND() as HIDDEN rand FROM YourBundle:Ent e ORDER BY rand"); ?>