mapado/mysql-doctrine-functions

Doctrine 的 MySQL 函数:RAND(), ROUND(), DATE(), DATE_FORMAT()...

v1.1.3 2015-08-15 15:18 UTC

This package is auto-updated.

Last update: 2024-09-15 09:21:16 UTC


README

此库为您提供 Doctrine2 的 MySQL 函数。

目前支持的函数有

  • RAND
  • ROUND
  • DATE
  • DATE_FORMAT

欢迎您分支此库并添加其他函数。

安装

获取包

使用 composer

composer require mapado/mysql-doctrine-functions

将类添加到您的配置中

$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction('rand', \Mapado\MysqlDoctrineFunctions\DQL\MysqlRand::class);
$config->addCustomStringFunction('round', \Mapado\MysqlDoctrineFunctions\DQL\MysqlRound::class);
$config->addCustomStringFunction('date', \Mapado\MysqlDoctrineFunctions\DQL\MysqlDate::class);
$config->addCustomStringFunction('date_format', \Mapado\MysqlDoctrineFunctions\DQL\MysqlDateFormat::class);

$em = EntityManager::create($dbParams, $config);

当然,您也可以仅选择需要的函数。

与 Symfony 一起使用

如果您在 Symfony 应用程序中安装了此库,您可以在您的 config.yml 文件中添加以下内容(如果您使用 symfony flex,则为 doctrine.yaml 文件)

# app/config/config.yml
doctrine:
    orm:
        # ...
        entity_managers:
            default:
                # ...
                dql:
                    numeric_functions:
                        rand:        'Mapado\MysqlDoctrineFunctions\DQL\MysqlRand'
                        round:       'Mapado\MysqlDoctrineFunctions\DQL\MysqlRound'
                    datetime_functions:
                        date:        'Mapado\MysqlDoctrineFunctions\DQL\MysqlDate'
                        date_format: 'Mapado\MysqlDoctrineFunctions\DQL\MysqlDateFormat'
                    # ... add all functions you need

使用方法

现在您可以在您的 DQL 查询中使用这些函数了

$query = 'SELECT RAND(), ROUND(123.45) 
        FROM ...
    ';
$em->createQuery($query);