File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
Documentation/ApiOverview/Database/ExpressionBuilder Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,28 @@ is used.
236236 :language: php
237237 :caption: EXT:my_extension/Classes/Domain/Repository/MyTableRepository.php
238238
239+ .. _database-expression-builder-castText :
240+
241+ :php: `ExpressionBuilder::castText() `
242+ ------------------------------------
243+
244+ .. versionadded :: 13.3
245+
246+ .. todo: Add api description as soon as building code-snippets is working again
247+
248+ Can be used to create an expression which converts a value, row field value or
249+ the result of an expression to type `TEXT ` or a large `VARCHAR, depending on the
250+ database system in use.
251+
252+ Casting is done to large :sql: `VARCHAR/CHAR` types using the :sql: `CAST/CONVERT `
253+ or similar methods based on the used database engine.
254+
255+ .. include :: _EscapeWarning.rst.txt
256+
257+ .. literalinclude :: _RepositoryCastText.php
258+ :language: php
259+ :caption: EXT:my_extension/Classes/Domain/Repository/MyTableRepository.php
260+
239261.. _database-expression-builder-castVarchar :
240262
241263:php: `ExpressionBuilder::castVarchar() `
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace MyVendor \MyExtension \Domain \Repository ;
6+
7+ use TYPO3 \CMS \Core \Database \ConnectionPool ;
8+
9+ final class MyTableRepository
10+ {
11+ private const TABLE_NAME = 'my_table ' ;
12+
13+ public function __construct (private readonly ConnectionPool $ connectionPool ) {}
14+
15+ public function demonstrateCastText (string $ col1 , string $ col2 ): array
16+ {
17+ $ queryBuilder = $ this ->connectionPool
18+ ->getQueryBuilderForTable (self ::TABLE_NAME );
19+
20+ return $ queryBuilder
21+ ->selectLiteral (
22+ $ queryBuilder ->quoteIdentifier ($ col1 ),
23+ $ queryBuilder ->quoteIdentifier ($ col2 ),
24+ // !!! Escape all values passed to castText to prevent SQL injections
25+ $ queryBuilder ->expr ()->castText (
26+ $ queryBuilder ->quoteIdentifier ($ col2 ) . ' * 10 ' ,
27+ 'virtual_field ' ,
28+ ),
29+ )
30+ ->from (self ::TABLE_NAME )
31+ ->executeQuery ()
32+ ->fetchAllAssociative ();
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments