Doctrine2:在 QueryBuilder 中使用日期函数
Doctrine2: Use date functions in QueryBuilder
我想知道如何在查询生成器中使用 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;
您可以使用 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
我想知道如何在查询生成器中使用 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;
您可以使用 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