Bug #75952
closedPrepared statements fail for a configured second database
0%
Description
I am using the following dbal configuration:
https://gist.github.com/DenisMir/e98498d41652843a63a770af5d293683
When trying to execute a prepared statement like this:
https://gist.github.com/DenisMir/63064fc49ff9c5d272a07c79f56dd618
I'm getting a "TYPO3\CMS\Core\Database\PreparedStatement" object which contains a "statement" property which is null. This leads to "$row = $preparedQuery->fetch();" resulting in a "Call to a member function fetch() on null" in the "PreparedStatement.php on line 429".
I have tracked down the error to the dbal "DatabaseConnection" method "prepare_PREPAREDquery". This method does call its parent method on line 1568 like "parent::prepare_PREPAREDquery". The parent method looks like this:
public function prepare_PREPAREDquery($query, array $queryComponents) { if (!$this->isConnected) { $this->connectDB(); } $stmt = $this->link->stmt_init(); $success = $stmt->prepare($query); if ($this->debugOutput) { $this->debug('stmt_execute', $query); } return $success ? $stmt : null; }
It does use the "default" link which does not know the table from the second configured database. This results in "$stmt->prepare($query);" failing for tables that exist in a different database. A possible implementation would be inside the dbal DatabaseConnection class and would use the correct link depending on the table.
Something like this does work:
public function prepare_PREPAREDquery($query, array $queryComponents) { if (!$this->isConnected) { $this->connectDB(); } $stmt = $this->handlerInstance[$this->lastHandlerKey]['link']->stmt_init(); $success = $stmt->prepare($query); if ($this->debugOutput) { $this->debug('stmt_execute', $query); } return $success ? $stmt : null; }
I'm not an expert in the dbal extension nor in TYPO3 core so this was all I could collect from debugging the single steps.
It would be great if this bug could be fixed since it doesn't allow prepared statements when working with multiple databases in dbal.
Updated by Morton Jonuschat over 7 years ago
- Category changed from 999 to Database API (Doctrine DBAL)
- Status changed from New to Closed
Solved on TYPO3 8/master by the migration to Doctrine DBAL.