[TYPO3-mvc] How to compare a DateTime object correctly in an Extbase query?
Jan Kornblum
jan.kornblum at gmx.de
Tue Jan 6 12:17:59 CET 2015
Dear newsgroup,
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?
Kind regards, Jan
More information about the TYPO3-project-typo3v4mvc
mailing list