├── DQL ├── MysqlDate.php ├── MysqlDateFormat.php ├── MysqlRand.php ├── MysqlRound.php └── MysqlSoundex.php ├── LICENSE ├── README.md └── composer.json /DQL/MysqlDate.php: -------------------------------------------------------------------------------- 1 | dateExpression->dispatch($sqlWalker) . ')'; 32 | } 33 | 34 | /** 35 | * parse 36 | * 37 | * @param \Doctrine\ORM\Query\Parser $parser 38 | * @access public 39 | * @return void 40 | */ 41 | public function parse(\Doctrine\ORM\Query\Parser $parser) 42 | { 43 | $parser->match(Lexer::T_IDENTIFIER); 44 | $parser->match(Lexer::T_OPEN_PARENTHESIS); 45 | $this->dateExpression = $parser->StringPrimary(); 46 | $parser->match(Lexer::T_CLOSE_PARENTHESIS); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /DQL/MysqlDateFormat.php: -------------------------------------------------------------------------------- 1 | walkArithmeticExpression($this->dateExpression) . 39 | ','. 40 | $sqlWalker->walkStringPrimary($this->formatChar) . 41 | ')'; 42 | } 43 | 44 | /** 45 | * parse 46 | * 47 | * @param \Doctrine\ORM\Query\Parser $parser 48 | * @access public 49 | * @return void 50 | */ 51 | public function parse(\Doctrine\ORM\Query\Parser $parser) 52 | { 53 | $parser->match(Lexer::T_IDENTIFIER); 54 | $parser->match(Lexer::T_OPEN_PARENTHESIS); 55 | $this->dateExpression = $parser->ArithmeticExpression(); 56 | $parser->match(Lexer::T_COMMA); 57 | $this->formatChar = $parser->StringPrimary(); 58 | $parser->match(Lexer::T_CLOSE_PARENTHESIS); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /DQL/MysqlRand.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class MysqlRand extends FunctionNode 14 | { 15 | /** 16 | * getSql 17 | * 18 | * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker 19 | * @access public 20 | * @return string 21 | */ 22 | public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 23 | { 24 | return 'RAND()'; 25 | } 26 | 27 | /** 28 | * parse 29 | * 30 | * @param \Doctrine\ORM\Query\Parser $parser 31 | * @access public 32 | * @return void 33 | */ 34 | public function parse(\Doctrine\ORM\Query\Parser $parser) 35 | { 36 | $parser->match(Lexer::T_IDENTIFIER); 37 | $parser->match(Lexer::T_OPEN_PARENTHESIS); 38 | $parser->match(Lexer::T_CLOSE_PARENTHESIS); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DQL/MysqlRound.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class MysqlRound extends FunctionNode 16 | { 17 | /** 18 | * simpleArithmeticExpression 19 | * 20 | * @var mixed 21 | * @access public 22 | */ 23 | public $simpleArithmeticExpression; 24 | 25 | /** 26 | * roundPrecision 27 | * 28 | * @var mixed 29 | * @access public 30 | */ 31 | public $roundPrecision; 32 | 33 | /** 34 | * getSql 35 | * 36 | * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker 37 | * @access public 38 | * @return string 39 | */ 40 | public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 41 | { 42 | return sprintf( 43 | 'ROUND(%s, %s)', 44 | $sqlWalker->walkSimpleArithmeticExpression($this->simpleArithmeticExpression), 45 | (is_null($this->roundPrecision) ? 0 : $sqlWalker->walkStringPrimary($this->roundPrecision)) 46 | ); 47 | } 48 | 49 | /** 50 | * parse 51 | * 52 | * @param \Doctrine\ORM\Query\Parser $parser 53 | * @access public 54 | * @return void 55 | */ 56 | public function parse(\Doctrine\ORM\Query\Parser $parser) 57 | { 58 | $parser->match(Lexer::T_IDENTIFIER); 59 | $parser->match(Lexer::T_OPEN_PARENTHESIS); 60 | 61 | $this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression(); 62 | 63 | try { 64 | $parser->match(Lexer::T_COMMA); 65 | $this->roundPrecision = $parser->ArithmeticExpression(); 66 | } catch(QueryException $e) { 67 | // ROUND() is being used without round precision 68 | } 69 | 70 | $parser->match(Lexer::T_CLOSE_PARENTHESIS); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /DQL/MysqlSoundex.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class MysqlSoundex extends FunctionNode 14 | { 15 | 16 | /* 17 | * Holds the string of the SOUNDEX statement 18 | * @var mixed 19 | */ 20 | protected $stringExpression; 21 | 22 | /** 23 | * getSql 24 | * 25 | * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker 26 | * @access public 27 | * @return string 28 | */ 29 | public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 30 | { 31 | return 'SOUNDEX(' . $this->stringExpression->dispatch($sqlWalker) . ')'; 32 | } 33 | 34 | /** 35 | * parse 36 | * 37 | * @param \Doctrine\ORM\Query\Parser $parser 38 | * @access public 39 | * @return void 40 | */ 41 | public function parse(\Doctrine\ORM\Query\Parser $parser) 42 | { 43 | $parser->match(Lexer::T_IDENTIFIER); 44 | $parser->match(Lexer::T_OPEN_PARENTHESIS); 45 | $this->stringExpression = $parser->StringPrimary(); 46 | $parser->match(Lexer::T_CLOSE_PARENTHESIS); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Julien Deniau 2 | Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MySQL Doctrine functions 2 | ==================== 3 | 4 | This library provides you MySQL functions for Doctrine2. 5 | 6 | At the moment are supported 7 | 8 | - RAND 9 | - ROUND 10 | - DATE 11 | - DATE_FORMAT 12 | 13 | Feel free to fork and add other functions. 14 | 15 | ## Installation 16 | 17 | ### Get the package 18 | 19 | With [composer](https://getcomposer.org/) 20 | 21 | ```sh 22 | composer require mapado/mysql-doctrine-functions 23 | ``` 24 | 25 | ### Add the classes to your configuration 26 | 27 | ```php 28 | $config = new \Doctrine\ORM\Configuration(); 29 | $config->addCustomStringFunction('rand', \Mapado\MysqlDoctrineFunctions\DQL\MysqlRand::class); 30 | $config->addCustomStringFunction('round', \Mapado\MysqlDoctrineFunctions\DQL\MysqlRound::class); 31 | $config->addCustomStringFunction('date', \Mapado\MysqlDoctrineFunctions\DQL\MysqlDate::class); 32 | $config->addCustomStringFunction('date_format', \Mapado\MysqlDoctrineFunctions\DQL\MysqlDateFormat::class); 33 | 34 | $em = EntityManager::create($dbParams, $config); 35 | ``` 36 | You can of course pick just the functions you need. 37 | 38 | ### Use with Symfony 39 | If you install the library in a Symfony application, you can add this in your `config.yml` file (`doctrine.yaml` file if you use symfony flex) 40 | 41 | ```yaml 42 | # app/config/config.yml 43 | doctrine: 44 | orm: 45 | # ... 46 | entity_managers: 47 | default: 48 | # ... 49 | dql: 50 | numeric_functions: 51 | rand: 'Mapado\MysqlDoctrineFunctions\DQL\MysqlRand' 52 | round: 'Mapado\MysqlDoctrineFunctions\DQL\MysqlRound' 53 | datetime_functions: 54 | date: 'Mapado\MysqlDoctrineFunctions\DQL\MysqlDate' 55 | date_format: 'Mapado\MysqlDoctrineFunctions\DQL\MysqlDateFormat' 56 | # ... add all functions you need 57 | ``` 58 | 59 | ### Usage 60 | You can now use the functions in your DQL Query 61 | 62 | ```php 63 | $query = 'SELECT RAND(), ROUND(123.45) 64 | FROM ... 65 | '; 66 | $em->createQuery($query); 67 | 68 | ``` 69 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mapado/mysql-doctrine-functions", 3 | "description": "MySQL Function for Doctrine : RAND(), ROUND() DATE(), DATE_FORMAT()...", 4 | "keywords": ["mapado", "mysql", "doctrine", "function"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Julien Deniau", 9 | "email": "julien.deniau@mapado.com" 10 | } 11 | ], 12 | "require": { 13 | "doctrine/orm": ">=2.2.0" 14 | }, 15 | "autoload": { 16 | "psr-0": { "Mapado\\MysqlDoctrineFunctions": "" } 17 | }, 18 | "target-dir": "Mapado/MysqlDoctrineFunctions" 19 | } 20 | --------------------------------------------------------------------------------