Search code examples
typo3extbasetypo3-6.2.x

TYPO3 - Scheduler task with custom query


I'm using TYPO3 6.2. I created a custom Extbase Task in order to execute it automatically every day.

<?php

namespace Myextension\Scheduler;

use TYPO3\CMS\Core\Utility\GeneralUtility;

class Task extends \TYPO3\CMS\Scheduler\Task\AbstractTask {


    public function execute()
    {

        // Custom MySQL query here

    }

}

?>

I need to develop a complicated mysql query with a lot of conditions, datas and joins : is it possible not to use $GLOBALS['TYPO3_DB']->exec_SELECTquery but a method like $GLOBALS['TYPO3_DB']->sql_query(' SELECT * FROM ... ') ?


Solution

  • [EDIT]: This answer is related to old and outdated versions of TYPO3 and not working anymore in current versions as the old abstraction layer $GLOBALS['TYPO3_DB'] was replaced by DBAL in TYPO3 v9.


    Yes, that's definitely possible.

    This is the complete function:

    /**
     * Executes query
     * MySQLi query() wrapper function
     * Beware: Use of this method should be avoided as it is experimentally supported by DBAL. You should consider
     * using exec_SELECTquery() and similar methods instead.
     *
     * @param string $query Query to execute
     * @return boolean|\mysqli_result|object MySQLi result object / DBAL object
     */
    public function sql_query($query) {
        $res = $this->query($query);
        if ($this->debugOutput) {
            $this->debug('sql_query', $query);
        }
        return $res;
    }
    

    And you find it in the file:

    typo3/sysext/core/Classes/Database/DatabaseConnection.php
    

    And you can call it like you wrote:

    $GLOBALS['TYPO3_DB']->sql_query(' SELECT * FROM ... ')