Skip to content

Commit bc0f4d5

Browse files
authored
[FEATURE] Add castText() expression support to ExpressionBuilder (#5350)
* [FEATURE] Add castText() expression support to ExpressionBuilder Resolves: TYPO3-Documentation/Changelog-To-Doc#978 Releases: main, 13.4 * [FEATURE] Add castText() expression support to ExpressionBuilder Resolves: TYPO3-Documentation/Changelog-To-Doc#978 Releases: main, 13.4
1 parent 8bbf4a3 commit bc0f4d5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Documentation/ApiOverview/Database/ExpressionBuilder/Index.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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()`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)