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.

Revisions

  1. @einpraegsam einpraegsam revised this gist Jun 16, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SelectExample1.php
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // select * from tt_content where hidden=0 and colPos=0 limit 3;
    // select * from tt_content where colPos=0 limit 3;
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $result = $queryBuilder
    ->select('*')
    ->from('tt_content')
    ->where('hidden = 0 and colPos = 0')
    ->where('colPos=0')
    ->orderBy('sorting')
    ->setMaxResults(3)
    ->execute();
  2. @einpraegsam einpraegsam renamed this gist Jun 15, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @einpraegsam einpraegsam revised this gist Jun 15, 2017. No changes.
  4. @einpraegsam einpraegsam revised this gist Jun 15, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion InsertExample.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // insert into
    // insert into tt_content (header, crdate, pid) VALUES ("New content", 123456789, 123);
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $queryBuilder
    ->insert('tt_content')
  5. @einpraegsam einpraegsam revised this gist Jun 15, 2017. 5 changed files with 21 additions and 2 deletions.
    3 changes: 2 additions & 1 deletion InsertExample.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // insert into
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $queryBuilder
    ->insert('tt_content')
    @@ -10,4 +11,4 @@
    'crdate' => time(),
    'pid' => 123,
    ])
    ->execute();
    ->execute();
    5 changes: 4 additions & 1 deletion Introduction.txt
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,7 @@ 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.
    for me and of course for everybody who wants to know.

    Find out more in the documentation: https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/QueryBuilder/Index.html
    or in the PHP Class \TYPO3\CMS\Core\Database\Query\QueryBuilder
    1 change: 1 addition & 0 deletions SelectExample1.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // select * from tt_content where hidden=0 and colPos=0 limit 3;
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $result = $queryBuilder
    ->select('*')
    1 change: 1 addition & 0 deletions SelectExample2.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // select header from tt_content where colPos=0;
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $result = $queryBuilder
    ->select('header')
    13 changes: 13 additions & 0 deletions UpdateExample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    // update tt_content set header = "new" where uid = 120;
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $queryBuilder
    ->update('tt_content')
    ->where(
    $queryBuilder->expr()->eq('uid', 120) // if 120 would be a user parameter, use $queryBuilder->createNamedParameter($param) for security reasons
    )
    ->set('header', 'new')
    ->execute();
  6. @einpraegsam einpraegsam revised this gist Jun 15, 2017. 2 changed files with 20 additions and 3 deletions.
    13 changes: 13 additions & 0 deletions InsertExample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $queryBuilder
    ->insert('tt_content')
    ->values([
    'header' => 'New content',
    'crdate' => time(),
    'pid' => 123,
    ])
    ->execute();
    10 changes: 7 additions & 3 deletions Introduction.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    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.
    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()
    @@ -9,4 +12,5 @@ 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.
    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.
  7. @einpraegsam einpraegsam revised this gist Jun 15, 2017. 3 changed files with 18 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion Introduction.txt
    Original file line number Diff line number Diff line change
    @@ -1 +1,12 @@
    My Text
    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.
    3 changes: 3 additions & 0 deletions SelectExample1.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    <?php
    use TYPO3\CMS\Core\Database\ConnectionPool;
    use TYPO3\CMS\Core\Utility\GeneralUtility;

    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $result = $queryBuilder
    ->select('*')
    3 changes: 3 additions & 0 deletions SelectExample2.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    <?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')
  8. @einpraegsam einpraegsam revised this gist Jun 15, 2017. 4 changed files with 14 additions and 2 deletions.
    1 change: 0 additions & 1 deletion Example.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    My text
    1 change: 1 addition & 0 deletions Introduction.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    My Text
    4 changes: 3 additions & 1 deletion Example.php → SelectExample1.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
    $result = $queryBuilder
    ->select('*')
    @@ -6,4 +7,5 @@
    ->orderBy('sorting')
    ->setMaxResults(3)
    ->execute();
    $rows = $result->fetchAll();
    $rows = $result->fetchAll();
    var_dump($rows);
    10 changes: 10 additions & 0 deletions SelectExample2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php
    $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);
    }
  9. @einpraegsam einpraegsam created this gist Jun 15, 2017.
    9 changes: 9 additions & 0 deletions Example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    $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();
    1 change: 1 addition & 0 deletions Example.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    My text