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
andX()
there because you only have 1 expression. If not, show us the exact query. We need to know if there are things like JOIN clauses, or if you're using the Doctrine Paginator, etc... – AppliqueandX
, which Jasper mentioned), is that the DB column is named differently than the entity property. – Owinge.dateStart
; and could you identify theactual dbms data type
of that field (not what you see in your ORM) – Ackley