在LiipFunctionalTest捆绑包中使用年、月、日mysql函数时出错
我正在使用mysql的函数()和LiipFunctionalTestBundle。当我运行测试时,我得到:在LiipFunctionalTest捆绑包中使用年、月、日mysql函数时出错,mysql,unit-testing,symfony,doctrine-orm,liipfunctionaltestbundle,Mysql,Unit Testing,Symfony,Doctrine Orm,Liipfunctionaltestbundle,我正在使用mysql的函数()和LiipFunctionalTestBundle。当我运行测试时,我得到: SQLSTATE[HY000]:一般错误:1无此类函数:年份 最后,测试失败了。我想是因为这个 这是我的配置测试文件: imports: - { resource: config_dev.yml } framework: test: ~ session: storage_id: session.storage.filesystem
SQLSTATE[HY000]:一般错误:1无此类函数:年份
最后,测试失败了。我想是因为这个
这是我的配置测试文件:
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.filesystem
name: MOCKSESSID
web_profiler:
toolbar: false
intercept_redirects: false
swiftmailer:
disable_delivery: true
liip_functional_test:
cache_sqlite_db: true
authentication:
username: "root"
password: "admin"
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
path: %kernel.cache_dir%/test.db
orm:
dql:
datetime_functions:
YEAR: DoctrineExtensions\Query\Mysql\Year
MONTH: DoctrineExtensions\Query\Mysql\Month
DAY: DoctrineExtensions\Query\Mysql\Day
在我的代码中,我将它们用作:
$em = $this->getDoctrine()->getManager();
$emConfig = $em->getConfiguration();
$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');
....
$em = $this->get('doctrine.orm.entity_manager');
$dql = $em->createQuery('SELECT um FROM Acme\AcmeBundle\Entity\Menu um WHERE
um.user = :id and
YEAR(um.day) = :year and
MONTH(um.day) = :month and
DAY(um.day) = :day');
$dql->setParameter('id', $id);
$dql->setParameter('year', $now->format("Y"));
$dql->setParameter('month', $now->format("m"));
$dql->setParameter('day', $now->format("d"));
如果我运行应用程序,我就不会有问题。问题与mysql
无关,而是与sqlite
有关。我使用sqlite
运行测试,它抛出了上述异常。我发现解决这个问题的最简单方法是使用两个数据库都能理解的通用版本更改查询
我还认为每个查询都有两个版本,一个用于mysql
,另一个用于sqlite
,并在应用程序执行测试时使用环境。例如,如果我正在使用test
环境,请使用sqlite
查询,否则请使用mysql
。然而,我认为这个选项非常依赖,将来难以维护。 yml定义中的输入错误DoctrinceExtensions
而不是DoctrineExtensions
?当然。但它仍然不起作用。