TYPO3 6.2自定义仓库查询返回空结果及多条件添加求助
Hey there! Let's break down why your custom findByCustom() method is returning empty results, and how to add a second query condition.
First: Fix the Empty Result Issue
Since findByUid() works, your Repository and model mapping are mostly correct—so let's focus on the query itself. Here are the most likely fixes:
1. Check the Field Name in Your Query
Extbase queries use model property names, not raw database column names. If your Event model uses a camelCase property like $sysLanguageUid, you need to use 'sysLanguageUid' instead of 'sys_language_uid' in your equals() call:
public function findByCustom() { $query = $this->createQuery(); // Use the model property name, not database column name $query->matching($query->equals('sysLanguageUid', 0)); return $query->execute(); }
This is a common gotcha—Extbase automatically maps camelCase properties to snake_case database columns, so using the database name here will fail to match anything.
2. Test with Ignored Enable Fields
TYPO3 Extbase applies default enable field filters (like hidden, starttime, endtime) by default. If your events are hidden, scheduled for the future, or expired, they'll be filtered out. Temporarily ignore these filters to test:
public function findByCustom() { $query = $this->createQuery(); // Disable enable field checks temporarily to debug $query->getQuerySettings()->setIgnoreEnableFields(true); $query->matching($query->equals('sysLanguageUid', 0)); return $query->execute(); }
If this returns results, you'll know the issue is with enable fields. You can either:
- Ensure your events are set to visible (
hidden=0) and within their valid date range - Or selectively ignore specific enable fields if needed:
$query->getQuerySettings()->setEnableFieldsToIgnore(['hidden']);
3. Verify Your Event Records
Double-check that you actually have events with sys_language_uid = 0 (default language) in your database. Sometimes records get assigned to a different language ID accidentally.
Adding a Second Query Condition
To combine multiple conditions, use Extbase's andX() or orX() methods to group constraints. Here's how to do it:
Example: AND Condition (Both Must Be True)
Suppose you want to filter for default language and events that aren't hidden:
public function findByCustom() { $query = $this->createQuery(); // Define your individual constraints $languageConstraint = $query->equals('sysLanguageUid', 0); $visibleConstraint = $query->equals('hidden', 0); // Combine with AND logic $query->matching($query->andX($languageConstraint, $visibleConstraint)); return $query->execute(); }
Example: OR Condition (Either Can Be True)
If you want default language or a specific category ID:
public function findByCustom() { $query = $this->createQuery(); $languageConstraint = $query->equals('sysLanguageUid', 0); $categoryConstraint = $query->equals('category', 123); // Combine with OR logic $query->matching($query->orX($languageConstraint, $categoryConstraint)); return $query->execute(); }
You can add as many constraints as needed inside andX() or orX()—just separate them with commas.
内容的提问来源于stack exchange,提问作者Paolito75

