在表列(a. paymentDate)
中,日期以Y-m-d H:m:i
格式插入。我想查询特定日期的所有条目。为此,我必须将日期格式从 Y-m-d H:m:i
更改为 Y-m-d
。我的查询如下。
namespace Regal\SmsBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* DailyTransactionRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class DailyTransactionRepository extends EntityRepository
{
public function getDailyPayment($studentId,$paymentDate)
{
$paymentDate= new \DateTime('2013-03-11');
$query = $this->getEntityManager()->createQuery("
SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
FROM RegalSmsBundle:DailyTransaction a
WHERE DATE(a.paymentDate) = :paymentDate AND a.students = :studentId
")
->setParameter('studentId', $studentId)
->setParameter('paymentDate', $paymentDate->format('Y-m-d'))
;
return $query->getResult();
}
}
最佳答案
试试这个,
$query = $this->getEntityManager()->createQuery("
SELECT a.id, a.amont, a.paymentDescrip, a.paymentType, a.paymentDate
FROM RegalSmsBundle:DailyTransaction a
WHERE DATE(a.paymentDate) = :paymentDate AND a.students = :studentId
")
;
$compareTo = new \DateTime('2013-03-11');
$query->setParameters(array(
'studentId' => $studentId,
'PaymentData' => $compareTo->format('Y-m-d')),
));
return $query->getResult();
更新,
Date() 似乎无法识别。因此,为了让我分享的代码片段发挥作用。您应该添加 Custom DQL Function和 Register the extension让您的实体经理知道这一点。
看看the documentation ,解释得很好。
另外,
我认为可以使用DATE_DIFF(date1, date2)用于比较 - 它为您提供天数差异,这确实适合您的需要。
关于symfony - 更改symfony2中的学说查询中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15504709/