Frage

im using codeigniter with doctrine, this is my bd:

id fecha_publicacion
1  2013-03-23 14:52:06
2  2013-03-23 18:02:06
3  2013-03-23 00:00:00
..   ...

what i want to do, is to search those publications from a certain date or between a certain range of dates

The thing is that, if i want to select all those fields with the date 2013-03-23 my query only returns the 3erd field (which time is 00:00:00)

how can i do to get what i want? i've tried many things but had no success

public function postsByDates($tipo,$fecha1,$fecha2=''){
    if($fecha2 == ''){
        $fecha2 = $fecha1;
    }

    $this->qb = $this->em->createQueryBuilder();
    $this->qb->select('p')
    ->from('models\Post', 'p')
    ->where(
        $this->qb->expr()->eq('p.tipo', '?1'),
        $this->qb->expr()->between('p.fecha_publicacion', '?2', '?3')
            )   
    ->setParameter(1, $tipo)
    ->setParameter(2, "$fecha1%")
    ->setParameter(3, "$fecha2%");

    $query = $this->qb->getQuery();
    $obj = $query->getResult();

    return $obj;
}
War es hilfreich?

Lösung

The problem is you are supplying a date for a datetime/timestamp comparison. So I am guessing it converts your supplied date (2013-03-23) to a timestamp (2013-3-23 00:00:00).

Quick fix: You can have the two dates like "2013-03-23 00:00:00" and "2013-03-23 23:59:59".

Good solution: You DQL native query to call MySQL's date() function to convert your timestamp field (fecha_publicacion) to date. After that you will be able to use the between function.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top