【问题标题】:Symfony2: Unable to use DoctrineExtensions Mysql Year functionSymfony2:无法使用 DoctrineExtensions Mysql Year 函数
【发布时间】:2013-02-22 04:39:57
【问题描述】:

到目前为止,我未能成功实施 beberlei DoctrineExtensions。我需要 MySQL Year() 函数。错误是:

预期的已知函数,得到'年份'

所以我一定错过了什么。什么?

这是我目前得到的:

扩展安装在..\vendor\beberlei\lib\DoctrineExtensions

app/autoload.php 内容为:

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('DoctrineExtensions', __DIR__.'/../vendor/beberlei/lib/DoctrineExtensions');

// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

年份函数使用:

class ClientRepository extends EntityRepository {

    public function activeStatus($year, $status) {
        $em = $this->getEntityManager();
        $query = $em
                ->createQuery('select max(Year(ct.contact_date)) CY, 
                c.active
                from ManaClientBundle:Contacts ct
                join ManaClientBundle:Client c on ct.cid = c.id
                group by id')
            ->getResult();

        return $em->createQuery("select CY, count(CY) Status from
                $query where CY = $year and active = '$status'")
            ->getResult();
    }
}

【问题讨论】:

  • 您是否尝试过 YEAR 而不是 Year?
  • 是的。唯一的区别是错误消息说found YEAR

标签: php mysql symfony doctrine-orm


【解决方案1】:

您是否将 YEAR 函数注册到 Doctrine 配置中?

$this->getEntityManager()->getConfiguration()->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');

【讨论】:

  • 将此行添加到使用 Year 的函数会导致 Class 'DoctrineExtensions\Query\Mysql\Year' not found。如果我将函数盗版到我的包中并将其注册到 config.yml 中,也会发生此错误。
  • 您确定您的自动加载设置正确吗?我已经安装了composer,路径是vendor/beberlei/DoctrineExtensions/lib/DoctrineExtensions。在我的路径中注意额外的DoctrineExtensions。如果你是通过 composer 安装的,则无需手动将教义扩展添加到自动加载器中,因为它已经在 composer.json 中提供了 psr-0 配置
  • 我会把所有的东西都退出来试试那个方法;我手动完成了安装。不确定作曲家安装应该如何进行,但现在是尝试的好时机。
  • OK - 我不知道如何使用有关 DoctrineExtensions composer.json 的说明。我认为它会以某种方式被卷入 symfony composer.json,但不是把整个事情都写到最后!
  • 我不明白你的问题。通常,您在项目的根目录中创建一个 composer.json 并在那里设置所有项目依赖项。有很多教程如何做到这一点。如果你已经这样做了,你只需 cd 到你的项目根目录并从命令行调用 composer install。 Composer 下载所有包并将它们放在vendor 映射中,并为您设置所有自动加载。我不是 symfony 用户,但应该很简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多