Thursday, January 3, 2013

TYPO3 - get resulting SQL from Extbase query

When you develop a TYPO3 Extbase extension, you may sometimes wonder, why a query does not return the exprected results. In this situation, it would be great if you can get the resultig SQL from TYPO3.

Actually, there are some solutions on the internet for this problem. Sadly, the one I found here does not work for me in TYPO3 6.0. The code snippet found here pointed me to the right direction. There is only one problem with the snippet, since it does not contain the whole SQL query (some parameters are filled with a questionmark).

So here is hopefully a complete solution on how to output the resulting SQL from an Extbase query.

Attention: The following code changes should only be done for development purposes on development systems.

  1. Open the file "/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php"
  2. Navigate to the function "GetObjectDataByQuery()"
  3. Go to the line next to "$this->replacePlaceholders($sql, $parameters, $tableName);"
  4. Insert the following code 

var_dump($sql);

This should output the resulting SQL query. Please notice, that this change can result in more than just one SQL query output, since now every query processed by Extbase is printed out.

If anyone has another solution for this problem, feel free to drop me a message.

Happy debugging :-)


2 comments:

  1. are you fkin kidding me... so this is the way to debug sql queries in extbase?! i want my pibase back
    ...

    ReplyDelete
  2. you are right, there is no way to do it without modifying core/extbase. how can it be?
    for me is also necessary to use:
    $query->getQuerySettings()->setReturnRawQueryResult(TRUE);
    for this to work. without it the GetObjectDataByQuery() is not called at all. but I have to idea, what is instead...
    the more I work with extbase the more I think it's only complicated bunch of crap

    ReplyDelete