#105615Multiple conditions for QueryBuilder leftJoin not possible (again)
Description
In our current system (v11.5.41) it seems again not possible, to add multiple conditions in joins (see old and solved issue https://forge.typo3.org/issues/87411).
The reason is, that the condition parameter is again typed as string:
public function leftJoin(string $fromAlias, string $join, string $alias, ?string $condition = null): QueryBuilder
Probably in later versions like current v12 and v13 it is the same? How should we deal with this?
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 5
-
Garvin Hicking commented and changed status
NewNeeds Feedback 11/17/24, 16:44 -
Bastian Stargazer commented 11/18/24, 11:40
Thanks for your fast response. We’re using the core QueryBuilder, created by:
$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tablename');Indeed it works if we manually add a string-cast in front of the query-builder object.
Is this meant to be like this, or should toString() called automatically?->from('fe_users', 'u') ->leftJoin('u', 'example', 'e', (string)$qb->expr()->andX( $qb->expr()->eq('u.uid', 'e.fe_users_uid'), $qb->expr()->eq('e.field', 1), )) -
Garvin Hicking commented 11/18/24, 21:55
You’re welcome :)
In fact, yes – the T3v11 core did this using (string) too:
(Please note in TYPO3v12+, the andX() methods are replaced with simple and() methods, but the string cast remains).
If that’s ok with you, I would close the ticket?
-
Bastian Stargazer commented 11/19/24, 10:48
All right, if the core use the string-cast too, we’ll consider it as best-practice ;-)
I was just wondering, because everything becomes more and more type-save. So inside the join() method it would be easy to check if the given parameter is an object of type CompositeExpression (or ExpressionBuilder?!) and just call toString() in this case.
Anyway, casting is fine, just have to remember this case. Maybe a hint or example in the docs make sense. I mean, the current example (https://docs.typo3.org/permalink/t3coreapi:ApiOverview/Database/QueryBuilder/Index#join-innerjoin-rightjoin-and-leftjoin) could be easily extended by adding an and() condition as 4th parameter in this join(). But its extra-sugar.
> (Please note in TYPO3v12+, the andX() methods are replaced with simple and() methods, but the string cast remains).
> If that’s ok with you, I would close the ticket?Highly appreciate your feedback! 🙏🏼 And yes, you can close it.
-
Garvin Hicking commented and changed status
Needs FeedbackClosed 11/19/24, 13:31> I was just wondering, because everything becomes more and more type-save. So inside the join() method it would be easy to check if the given parameter is an object of type CompositeExpression (or ExpressionBuilder?!) and just call toString() in this case.
This is a good feeling. However for SQL, this is all string-based, so once a query gets built, this is the target format and it’s “ok” to resolve the specific objects into strings. You could even pass “AND/OR” join conditions strings yourself, but due to escaping/quoting that isn’t a good idea. Thus, using the (string) casting is really your best bet.
> Anyway, casting is fine, just have to remember this case. Maybe a hint or example in the docs make sense. I mean, the current example (https://docs.typo3.org/permalink/t3coreapi:ApiOverview/Database/QueryBuilder/Index#join-innerjoin-rightjoin-and-leftjoin) could be easily extended by adding an and() condition as 4th parameter in this join(). But its extra-sugar.
Yes, you are completely right! I’ve just added a PR to address this in the docs: https://github.com/TYPO3-Documentation/TYPO3CMS-Reference-CoreApi/pull/5062
Thanks!
Are you talking about the Extbase or core Querybuilder? What is your exact code that you use?
The querybuilder has __toString() casting that can convert things, so this shouldn’t happen when joins are properly built.