Skip to content

Commit ca626de

Browse files
franzholzsarahmccarthy123linawolf
committed
Add notes about RestrictionBuilder (#6167)
* Add notes about RestrictionBuilder The RestrictionBuilder should be mentioned on the top. It is used for every SQL query by default. * restrictions by default only with SELECT queries Clarified the note about RestrictionBuilder's default restrictions in select queries. * better explanation for `->where()` See https://symfonycasts.com/screencast/doctrine-queries/and-where-or-where andWhere() vs where() What's wrong with ->where()? Well, if you added a WHERE clause to your QueryBuilder earlier, calling ->where() would remove that and replace it with the new stuff... which probably isn't what you want. ->andWhere() always adds to the query. * make the SELECT default restrictions findable I have found another NOTE which explains the default restrictions. I have merged 2 NOTE boxes. * [DOCS] Language checks Releases: main * are defined * Apply suggestion from @linawolf * Add default restrictions link name I add an additional link name, because it is a default restriction. This topic has not been called select restriction in the past, I guess. --------- Co-authored-by: Sarah McCarthy <sarahmccarthy123@yahoo.com> Co-authored-by: Lina Wolf <48202465+linawolf@users.noreply.github.com> (cherry picked from commit 1f12b90)
1 parent 197de91 commit ca626de

File tree

1 file changed

+29
-25
lines changed
  • Documentation/ApiOverview/Database/DoctrineDbal/QueryBuilder

1 file changed

+29
-25
lines changed

Documentation/ApiOverview/Database/DoctrineDbal/QueryBuilder/Index.rst

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ an instance of the current query builder itself, and can be chained:
5252
->select('uid')
5353
->from('pages');
5454
55+
5556
.. _database-query-builder-instantiation:
5657

5758
Instantiation
@@ -83,7 +84,6 @@ injected via :ref:`dependency injection <DependencyInjection>`.
8384

8485
.. _significant performance penalty and memory consumption: https://www.derhansen.de/2023/10/the-pitfalls-of-reusing-typo3-querybuilder-analyzing-a-performance-bottleneck.html
8586

86-
8787
.. _database-query-builder-select:
8888

8989
select() and addSelect()
@@ -182,23 +182,24 @@ a query builder with the connection pool.
182182
See available :ref:`parameter types <database-connection-parameter-types>`.
183183

184184
.. _database-query-builder-select-restrictions:
185+
.. _database-query-builder-default-restrictions:
185186

186187
Default Restrictions
187188
--------------------
188189

189190
.. note::
190191
:php:`->select()` and :php:`->count()` queries trigger TYPO3 magic that adds
191-
further default where clauses if the queried table is also registered via
192-
:php:`$GLOBALS['TCA']`. See the :ref:`RestrictionBuilder
193-
<database-restriction-builder>` section for details on that topic.
194-
192+
default :php:`where` clauses to queries if they are defined as
193+
:ref:`enableColumns <t3tca:ctrl-enablecolumns>` in the table's TCA ctrl section.
194+
See the :ref:`RestrictionBuilder <database-restriction-builder>` section for
195+
further details.
195196

196197
.. _database-query-builder-count:
197198

198199
count()
199200
=======
200201

201-
Create a :sql:`COUNT` query, a typical usage:
202+
Create a :sql:`COUNT` query. Typical usage:
202203

203204
.. code-block:: php
204205
:caption: EXT:my_extension/Classes/Domain/Repository/MyRepository.php
@@ -225,11 +226,13 @@ See available :ref:`parameter types <database-connection-parameter-types>`.
225226

226227
Remarks:
227228

228-
* Similar to the :php:`->select()` query type, :php:`->count()` automatically
229-
triggers the magic of the :ref:`RestrictionBuilder
230-
<database-restriction-builder>` that adds default restrictions such as
231-
:sql:`deleted`, :sql:`hidden`, :sql:`starttime` and :sql:`endtime` when
232-
defined in TCA.
229+
* see :ref:`Default Restrictions <database-query-builder-default-restrictions>`
230+
231+
* Similar to the :php:`->select()` query type, :php:`->count()`
232+
triggers :ref:`RestrictionBuilder <database-restriction-builder>`
233+
magic that adds default restrictions such as
234+
:sql:`deleted`, :sql:`hidden`, :sql:`starttime` and :sql:`endtime` if
235+
they are defined in the TCA.
233236

234237
* Similar to :php:`->select()` query types, :php:`->executeQuery()` with
235238
:php:`->count()` returns a result object of type :php:`\Doctrine\DBAL\Result`.
@@ -407,7 +410,7 @@ Remarks:
407410

408411
* The API does not magically add `deleted = 0` or other restrictions, as is
409412
currently the case with :ref:`select
410-
<database-query-builder-select-restrictions>`, for example.
413+
<database-query-builder-default-restrictions>`.
411414
(See also :ref:`RestrictionBuilder <database-restriction-builder>`).
412415

413416

@@ -485,7 +488,7 @@ tables with an explicit :php:`->join()`.
485488
where(), andWhere() and orWhere()
486489
=================================
487490

488-
The three methods are used to create :sql:`WHERE` restrictions for :sql:`SELECT`,
491+
These three methods create :sql:`WHERE` restrictions for :sql:`SELECT`,
489492
:sql:`COUNT`, :sql:`UPDATE` and :sql:`DELETE` query types. Each argument is
490493
usually an :ref:`ExpressionBuilder <database-expression-builder>` object that
491494
is converted to a string on :php:`->executeQuery()` or
@@ -525,10 +528,13 @@ Read :ref:`how to correctly instantiate <database-query-builder-instantiation>`
525528
a query builder with the connection pool.
526529
See available :ref:`parameter types <database-connection-parameter-types>`.
527530

528-
Note the parenthesis of the above example: :php:`->andWhere()` encapsulates both
529-
:php:`->where()` and :php:`->orWhere()` with an additional restriction.
531+
.. note::
532+
The commented out section in the code above demonstrates how including an
533+
:php:`->andWhere()` leads to nesting of the :php:`->where()` and
534+
:php:`->orWhere()` clauses.
535+
530536

531-
Argument unpacking can become handy with these methods:
537+
Argument unpacking is useful as shown in the following methods:
532538

533539
.. code-block:: php
534540
:caption: EXT:my_extension/Classes/Domain/Repository/MyRepository.php
@@ -558,11 +564,9 @@ Remarks:
558564
**Always** use this when dealing with user input in expressions to protect
559565
the statement from SQL injections.
560566

561-
* :php:`->where()` should be called only once per query and resets all
562-
previously set :php:`->where()`, :php:`->andWhere()` and :php:`->orWhere()`
563-
expressions. A :php:`->where()` call after a previous :php:`->where()`,
564-
:php:`->andWhere()` or :php:`->orWhere()` usually indicates a bug or a
565-
rather weird code flow. Doing so is discouraged.
567+
* :php:`->where()` replaces all previously set :php:`->where()`,
568+
:php:`->andWhere()` and :php:`->orWhere()` expressions. It should therefore
569+
be called only once and at the beginning of a query to prevent unwanted behavior.
566570

567571
* When creating complex :sql:`WHERE` restrictions, :php:`->getSQL()` and
568572
:php:`->getParameters()` are helpful debugging tools to verify parenthesis
@@ -872,12 +876,12 @@ queries.
872876
be a performance improvement.
873877

874878
.. note::
875-
While technically possible, it is not recommended to send direct SQL queries
876-
as strings to the `union()` and `addUnion()` methods. We recommend to use a
879+
Although it is technically possible, it is not recommended to send direct SQL queries
880+
as strings to the `union()` and `addUnion()` methods. We recommend using a
877881
query builder.
878882

879883
If you decide to do so you **must** take care of quoting, escaping, and
880-
valid SQL Syntax for the database system in question. The `Default Restrictions <https://docs.typo3.org/permalink/t3coreapi:database-query-builder-select-restrictions>`_
884+
valid SQL Syntax for the database system in question. The `Default Restrictions <https://docs.typo3.org/permalink/t3coreapi:database-query-builder-default-restrictions>`_
881885
are **not applied** on that part.
882886

883887
Named placeholders, such as created by :php:`QueryBuilder::createNamedParameter()`
@@ -924,7 +928,7 @@ Line 41
924928
Line 50
925929
Named parameters must also be called on the outer most union query builder.
926930

927-
The `Default Restrictions <https://docs.typo3.org/permalink/t3coreapi:database-query-builder-select-restrictions>`_
931+
The `Default Restrictions <https://docs.typo3.org/permalink/t3coreapi:database-query-builder-default-restrictions>`_
928932
are applied to each subquery automatically.
929933

930934
.. _database-query-builder-setMaxResults:

0 commit comments

Comments
 (0)