[TYPO3-mvc] How to compare a DateTime object correctly in an Extbase query?

Jan Kornblum jan.kornblum at gmx.de
Mon Jan 12 10:01:00 CET 2015


Hello again,

This really is a strange behaviour but i can't find my mistake. The 
code below works for 95% of my records. But there are some records 
where it doesn't work (although all have a valid DateTime value)! In 
this case the "findNext()" always returns the same object as the object 
passed as a parameter.

Kind regards, Jan

> i've to compare a DateTime property in a repository query to implement a 
> "previous" / "next" navigation for a single view. What should the query look 
> like correctly? This is what i've done so far:
>
> public function findPrevious(\Vendor\Ext\Domain\Model\Post $post) {
> 	$query = $this->createQuery();
> 	$query->setOrderings(array('datePublished' => 
> \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
> 	$query->matching($query->greaterThan('datePublished', 
> $post->getDatePublished()->format('Y-m-d H:i:s')));
> 	$previousPost = $query->execute()->getFirst();
> 	return $previousPost;
> }
>
> public function findNext(\Vendor\Ext\Domain\Model\Post $post) {
> 	$query = $this->createQuery();
> 	$query->setOrderings(array('datePublished' => 
> \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
> 	$query->matching($query->lessThan('datePublished', 
> $post->getDatePublished()->format('Y-m-d 00:00:00')));
> 	$nextPost = $query->execute()->getFirst();
> 	return $nextPost;
> }
>
> But it only works when i use "->format('Y-m-d 00:00:00')" on the DateTime 
> object in the second query. Which "format" must be used to get an exact 
> comparison?




More information about the TYPO3-project-typo3v4mvc mailing list