Extbase - get created sql from query

后端 未结 8 1885
感动是毒
感动是毒 2020-12-01 21:35

i want to get some database tables from my typo3 extensions. The Extension is based on extbase.

The query always returns nothing but the data exists

I\'ve tr

相关标签:
8条回答
  • 2020-12-01 22:13
    $query = $this->createQuery();
    $query->getQuerySettings()->setReturnRawQueryResult(TRUE);
    $getHotelInfo = 'SELECT * FROM `my_table` WHERE field = ? ORDER BY date DESC LIMIT 1';
    return $query->statement($getHotelInfo)->execute();
    

    For executing query you have to write 'setReturnQueryResult' on your repository

    0 讨论(0)
  • 2020-12-01 22:21

    Improvement to biesiors answer:

    As Extbase replaces some placeholders after calling buildQuery(), you might prefer to place the debug output into getObjectDataByQuery(), just after $this->replacePlaceholders($sql, $parameters, $tableName);

    if (strpos($sql, "your_table_name.")) {
        debug($sql, 'my debug output');
    };
    

    Also, better use debug() instead of var_dump().
    [File: typo3\sysext\extbase\Classes\Persistence\Generic\Storage\Typo3DbBackend.php. Line 339 in version 6.1]:

    0 讨论(0)
  • 2020-12-01 22:22

    A cleaner way to debug your statements when using TYPO3 6.1 is to use the query parser of Typo3DbBackend.

    $parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend');
    $params = array();
    $queryParts = $parser->parseQuery($query, $params);
    
    \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('query', 'my_extension', 1, array('query' => $queryParts, 'params' => $params));
    

    The parser returns an array containing the different parts of the generated SQL statement.

    With TYPO3 6.2 the parseQuery method was moved to Typo3DbQueryParser and lost its second parameter.

    0 讨论(0)
  • 2020-12-01 22:32

    Check this snippet, although it's not very comfortable in use it helps a lot:

    in general you need this code at the end of the buildQuery(array $sql) method (*) - right before return $statement;

    if (in_array("your_table_name", $sql['tables'])) {
        var_dump($statement);
        print_r($statement);
    }
    

    (*) Class file:

    • TYPO3 ver.: 4.x: typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php
    • TYPO3 ver.: 6.x: typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php

    In 6.2.x ...

    You can try within \TYPO3\CMS\Core\Database\DatabaseConnection::exec_SELECTquery method, just add the condition after fetching the $query, like (trim is important!):

    public function exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '') {
        $query = $this->SELECTquery($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit);
    
        if (trim($from_table) == 'fe_users') {
            DebuggerUtility::var_dump($query);
        }
    
    // rest of method
    
    0 讨论(0)
  • 2020-12-01 22:35

    i suggest set this in typo3conf/LocalConfiguration.php file under 'SYS' array

    'SYS' => array(
    ......
    'displayErrors' => 1,
    'sqlDebug' => 1
    .......
    )
    

    and then write wrong field name in query intentionally and then execute code. this will show last query execute with error.

    0 讨论(0)
  • 2020-12-01 22:37

    In TYPO3 6.2 you can use Extbase DebuggerUtility to debug the query.

    Add this code before $query->execute():

    /** @var Typo3DbQueryParser $queryParser */
    $queryParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser');
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->parseQuery($query));
    
    0 讨论(0)
提交回复
热议问题