我想知道如何在查询构建器中使用WEEK
或MONTH
等日期函数。我正在使用Zend而不是Symfony。当我用WEEK尝试当前的代码时,我收到了这个错误:
Error: Expected known function, got 'WEEK'
这是我目前的代码:
<?php
namespace Repositories;
use Doctrine\ORM\EntityRepository;
/**
* Analytic
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class Analytic extends EntityRepository {
public function getSocialAnalytics($type){
$response = false;
if($type){
$qb = $this->_em->createQueryBuilder();
$qb
->select('a')
->from('\Entities\Analytic', 'a');
$qb->where(' a.type = :type ');
$qb->andWhere(' WEEK(NOW()) = WEEK(created) ');
$qb->setParameter('type', $type);
$response = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
}
return $response;
}
}
这是我的配置:
// Custom resource plugins inherit this sweet getOptions() method which will retrieveenter code here
// configuration settings from the application.ini file
$config = new Zend_Config($this->getOptions());
// Define the connection parameters
$options = array(
'connection' => array(
'driver' => "{$config->connection->driver}",
'host' => "{$config->connection->host}",
'dbname' => "{$config->connection->dbname}",
'user' => "{$config->connection->user}",
'password' => "{$config->connection->password}"
)
);
$configEm = new \Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ArrayCache;
$driverImpl = $configEm->newDefaultAnnotationDriver(
$config->connection->entities
);
$configEm->setMetadataCacheImpl($cache);
$configEm->setMetadataDriverImpl($driverImpl);
// Configure proxies
$configEm->setAutoGenerateProxyClasses(
$config->connection->proxies->generate
);
$configEm->setProxyNamespace($config->connection->proxies->ns);
$configEm->setProxyDir(
$config->connection->proxies->location
);
// Configure cache
$configEm->setQueryCacheImpl($cache);
$em = \Doctrine\ORM\EntityManager::create($options['connection'], $configEm);
Zend_Registry::set('em', $em);
return $em;
答案 0 :(得分:5)
您可以使用https://github.com/beberlei/DoctrineExtensions。将其添加到您的composer.json并更新您的config.yml
,如下所示
doctrine:
dbal:
...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
Year: DoctrineExtensions\Query\Mysql\Year
修改。你想要的实际功能:
WEEK: DoctrineExtensions\Query\Mysql\Week