Skip to content

Instantly share code, notes, and snippets.

@JPDesign
Forked from einpraegsam/InsertExample.php
Created February 26, 2021 05:08
Show Gist options
  • Select an option

  • Save JPDesign/e3fcad0cf40fc30d2b9307c82ab2766a to your computer and use it in GitHub Desktop.

Select an option

Save JPDesign/e3fcad0cf40fc30d2b9307c82ab2766a to your computer and use it in GitHub Desktop.
TYPO3 QueryBuilder example use
I'm inspired through the slides from the talk from Nicole (see https://de.slideshare.net/cpsitgmbh/certifuncation-2017-best-practices-extension-development-for-typo3-8-lts) and doctrine in TYPO3.
As far as I understand the variable $GLOBALS['TYPO3_DB'] which contains the DatabaseConnection class will not be available in future versions of TYPO3.
That means that common functions are not available any more:
exec_SELECTquery()
exec_SELECTgetRows()
exec_SELECTgetSingleRow()
exec_DELETEquery()
exec_UPDATEquery()
exec_INSERTquery()
Nevertheless there is a QueryBuilder class that can be used. I played a bit with it in TYPO3 8.7.1 and stored some examples for me and of course for everybody who wants to know.
<?php
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$result = $queryBuilder
->select('*')
->from('tt_content')
->where('hidden = 0 and colPos = 0')
->orderBy('sorting')
->setMaxResults(3)
->execute();
$rows = $result->fetchAll();
var_dump($rows);
<?php
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$result = $queryBuilder
->select('header')
->from('tt_content')
->where('colPos=0')
->execute();
while ($row = $result->fetch()) {
var_dump($row);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment