TYPO3 Repository新增查询问题:如何添加指定时间查询条件?
Got it, let's walk through how to smoothly add that date filter to your existing TYPO3 Repository. I'll break this down into practical, easy-to-follow steps so you can integrate it without breaking existing functionality.
Option 1: Add a Custom Query Method (Recommended)
The best practice is to create a new custom method in your Repository instead of modifying default methods like findAll(). This keeps your existing logic intact while adding the new filter.
Here's how to do it:
- Open your Repository class (e.g.,
YourEntityRepository.phpinClasses/Domain/Repository/of your extension). - Add a new method with a descriptive name (like
findByUpcomingDates()— adjust based on your use case):
use TYPO3\CMS\Extbase\Persistence\QueryInterface; class YourEntityRepository extends \TYPO3\CMS\Extbase\Persistence\Repository { public function findByUpcomingDates() { // Initialize your date constraint $date = new \DateTime('midnight'); $query = $this->createQuery(); // Your core filter logic $dateConstraint = $query->greaterThanOrEqual('date', $date->format('Y-m-d H:i:s')); // If you need to combine this with other existing constraints (e.g., status = active), use logical operators: // $statusConstraint = $query->equals('status', 1); // $combinedConstraint = $query->logicalAnd($dateConstraint, $statusConstraint); // $query->matching($combinedConstraint); // Apply the constraint $query->matching($dateConstraint); // Return the filtered results return $query->execute(); } }
Make It Flexible (Optional)
If you want to reuse this logic with different dates, modify the method to accept a \DateTime parameter:
public function findByDateGreaterThanOrEqual(\DateTime $date) { $query = $this->createQuery(); $constraint = $query->greaterThanOrEqual('date', $date->format('Y-m-d H:i:s')); $query->matching($constraint); return $query->execute(); }
You can then call it like this in your controller:
$customDate = new \DateTime('next week'); $filteredItems = $this->yourEntityRepository->findByDateGreaterThanOrEqual($customDate);
Option 2: Modify a Default Method (Use Sparingly)
If you want the date filter to apply to a default method like findAll(), you can override it — but be warned: this changes the default behavior for every call to findAll().
public function findAll() { $date = new \DateTime('midnight'); $query = $this->createQuery(); $constraint = $query->greaterThanOrEqual('date', $date->format('Y-m-d H:i:s')); $query->matching($constraint); return $query->execute(); }
Key Notes to Remember
- Ensure your entity's
dateproperty is correctly mapped: In your entity class (e.g.,YourEntity.php), thedateproperty should be annotated with@var \DateTimeand mapped to adatetimedatabase field. - Clear TYPO3's cache after making changes to the Repository — this ensures the new method is recognized.
How to Use the New Method in Your Controller
Inject the Repository into your controller (if not already done), then call the custom method:
/** * @var \Vendor\YourExtension\Domain\Repository\YourEntityRepository */ protected $yourEntityRepository; // Inject via constructor or @inject annotation public function __construct(YourEntityRepository $yourEntityRepository) { $this->yourEntityRepository = $yourEntityRepository; } public function listAction() { $filteredItems = $this->yourEntityRepository->findByUpcomingDates(); $this->view->assign('items', $filteredItems); }
内容的提问来源于stack exchange,提问作者Stigi

