symfony - 更改symfony2中的学说查询中的日期格式

标签 symfony

在表列(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 FunctionRegister the extension让您的实体经理知道这一点。

看看the documentation ,解释得很好。

另外,

我认为可以使用DATE_DIFF(date1, date2)用于比较 - 它为您提供天数差异,这确实适合您的需要。

关于symfony - 更改symfony2中的学说查询中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15504709/

相关文章:

orm - Symfony2 使用哪个 ORM?

fosuserbundle - 如何访问注入(inject)服务中的用户 token 以重新编码密码?

php - 未找到 Symfony 类

symfony - Doctrine2 symfony2 多重多对多关系

doctrine-orm - 如何将实体管理器添加到自定义类或服务?

email - 如何使用 SwiftMailer (Symfony2) 在 html 格式的电子邮件中添加图片

php - 如何基于关系实现 Doctrine2 过滤器?

php - Symfony2 Controller 中的可重用函数

symfony - sfUser 等价于 Symfony 2

php - Symfony 4 : Reusable code in "src"