在Doctrine 2中如何ORDER BY DateTime? [英] How to ORDER BY DateTime in Doctrine 2?

查看:190
本文介绍了在Doctrine 2中如何ORDER BY DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下查询:

  $ qb = $ this-> getEntityManager() - > ; createQueryBuilder(); 

$ qb-> select('e')
- > from('Entity\Event','e')
- > setMaxResults($ limit )
- > setFirstResult($ offset)
- > orderBy('e.dateStart','ASC');

$ events = $ qb-> getQuery() - > getResult();

其中

  / ** 
*用户
*
* @ ORM\Table(name =event)
* @ ORM\Entity(repositoryClass =Repositories \EventRepository)
* /
class Event
{
/ **
* @var \DateTime
*
* @ ORM\Column(name =date_start,type =datetime,precision = 0,scale = 0,nullable = true,unique = false)
* /
private $ dateStart;

...
}

但是,不工作
我的结果不会在日期开始显示。



我正在寻找从最早到最新的



我该怎么做?



谢谢



编辑: / p>

按照上一个答案,我正在更新我的查询。
不幸的是我还是不能工作。
请帮助

  $ qb-> select('e')
- > from( 'Entity\Event','e')
- >其中(
$ qb-> expr() - > andX(
$ qb-> expr() >之间('e.dateStart',':from',':')


- > orderBy('e.dateStart','ASC')
- > setFirstResult($ offset)
- > setMaxResults($ limit);

谢谢



编辑2:
似乎移动orderBy确实有所作为。我现在没有任何错误。脚本运行正常,orderBy但是它不是由datetime所有的订单!



在我的结果中,我看不到任何会让我认为已经被命令基于任何给定的属性,绝对不是datetime!



如何可能?



Datetime字段看起来像在DB中的这样的事情:
2014-05-24 19:30:00



当我var转储从上一个查询出来的查询时,这里是我的datetie字段:

  [dateStart] => string(8)DateTime

这是否意味着它是一个原则的字符串,这就是为什么不按日期时间排序?



谢谢

解决方案

数据库



您应该检查数据库中的表结构



什么类型的列是 date_start

应该是 DATETIME (在MySQL中,其他供应商可能会有所不同)。 >
但我怀疑它是某种形式的字符串,如 VARCHAR (或 CHAR TEXT 等)。



在这种情况下,结果被排序,但不是在你期望的方式这是因为数据库的不同类型有所不同。



CLI工具



Doctrine带有一个控制台工具这可以验证您的映射,并检查数据库是否与它们一致: doctrine orm:validate-schema



如果它报告与数据库的不一致,请使用 doctrine orm:schema-tool:update --dump-sql 来显示更新数据库所执行的查询(它将假设映射是真相的根源)。


I'm looking to execute the following query:

        $qb = $this->getEntityManager()->createQueryBuilder();

        $qb->select( 'e' )
            ->from( 'Entity\Event',  'e' )
            ->setMaxResults( $limit )
            ->setFirstResult( $offset )
            ->orderBy('e.dateStart', 'ASC');

        $events = $qb->getQuery()->getResult();

Where

/**
 * User
 *
 * @ORM\Table(name="event")
 * @ORM\Entity(repositoryClass="Repositories\EventRepository")
 */
class Event
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_start", type="datetime", precision=0, scale=0, nullable=true, unique=false)
     */
    private $dateStart;

...
}

But the order by doesn't work. My results are not displayed by date start.

I'm looking to retrieve the 20 first events happening from the soonest to the latest

How can I do this ?

Thanks

EDIT:

Following the previous answer, I'm updating my query. unfortunatey I still can't have it working. Please help

     $qb->select( 'e' )
        ->from( 'Entity\Event',  'e' )
        ->Where( 
            $qb->expr()->andX(
                $qb->expr()->between('e.dateStart', ':from', ':to')
            )
        )
        ->orderBy('e.dateStart', 'ASC')
        ->setFirstResult( $offset )
        ->setMaxResults( $limit );

Thanks

EDIT 2: It seems moving orderBy did make a difference. I don't have any error as of right now. The script is running fine with orderBy BUT it is NOT ORDERED by datetime at all !

In my results I can't see anything that would make me think it has been ordered based on any given property, definitively not datetime !

How is that possible ?

The Datetime field looks like something like that in the DB: 2014-05-24 19:30:00

When I var Dump the queries that comes out of the previous query, here is what I have for the datetie field:

 ["dateStart"]=> string(8) "DateTime"

Does that mean it's really a string for doctrine and that's why it is not sorted by datetime ?

Thanks

解决方案

Database

You should check the table structure in the database:

What type of column is date_start?
It should be DATETIME (in MySQL, other vendors may vary).
But I suspect it is some form of string, like VARCHAR (or CHAR, TEXT, etc).

When this is the case, the result is ordered, but not in the way you expect it. This is because different types are ordered differently by the database.

CLI tool

Doctrine comes with a console tool that's able to validate your mappings and check if the database is consistent with them: doctrine orm:validate-schema.

If it reports inconsistencies with the database, use doctrine orm:schema-tool:update --dump-sql to have it display the queries it would perform to update the database (it will assume the mappings are the source of truth).

这篇关于在Doctrine 2中如何ORDER BY DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆