【问题标题】:Select records from start of month to current date using Doctrine使用 Doctrine 选择从月初到当前日期的记录
【发布时间】:2012-09-21 03:13:01
【问题描述】:

我在从学说中获取数据时遇到 2 个问题:

  1. 选择从月初到当前日期的记录
  2. 选择从年初到当前日期的记录

我怎样才能写一个 Doctrine 查询来做和我们一样的事情 in Mysql

这是我存储库中不完整的代码?

public function findByYearlyAttendanceTillToday($id)
{
    $em = $this->getEntityManager();
    $query = $em->createQuery("SELECT count(a) FROM CollegeStudentBundle:StudentAttendance a where a.present LIKE 'A' and a.student_id = :id and a.date > :date");
    $query->setParameter('id', $id);
    $query->setParameter('date', **?????**);
    return $query->getResult();
}

【问题讨论】:

    标签: symfony doctrine doctrine-orm


    【解决方案1】:

    这是你需要的:

    new \DateTime('midnight first day of this month');
    new \DateTime('first day of january');
    

    我也会改变这个:

    a.date > :date
    

    到这里:

    a.date >= :date
    

    【讨论】:

    • 太棒了.. 它工作了.. 你能告诉我任何参考资料,我可以从中读取我可以通过 new \DateTime(''); 传递的所有其他参数;
    • 非常感谢先生,但我在任何地方都找不到“本月第一天午夜”的说法。
    • 感谢您的回答。我这里还有一个问题。 stackoverflow.com/questions/10159598/…可以看看吗
    • new \DateTime('first day of today')new \DateTime('midnight first day of this month') 的简短替代品。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多