How to view db queries generated by TableGateway in Zend Framework 2

I am new to ZF2 and trying to use tableGateway to manage and update records in a database. I can select and update items without problems, but when I insert, I get an error. Since the tableGateway class creates a query on the fly, how can I see the query itself?

$this->tableGateway->insert($data); 

An error has occurred at runtime; Please try again later. Additional information: Zend \ Db \ adapter \ Exception \ InvalidQueryException

File:

 /[redacted]/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Statement.php:220 

Message:

 Statement could not be executed 
+4
source share
3 answers

A very elegant way to see db queries is to use zend-developer tools.

The easiest way to use it is to install the module by adding it to the composer.json file

..... "repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" } ], "require": { "php": ">=5.3.3", "zendframework/zend-developer-tools": "dev-master" }

see documentation

+2
source

Just some notifications for @ zdenek-machek's answer:

1) To request database queries, the BjyProfiler module must be installed.

2) (skip if you use PDO) When I used BjyProfiler last time, there was a problem with mysqli connection ( buffer_results option buffer_results not passed to ProfilingStatement class). Perhaps this has been fixed now, or I have configured it incorrectly, but my patch should manually pass this parameter to BjyProfiler / src / BjyProfiler / Db / Adapter / ProfilingAdapter.php:

 case 'Zend\Db\Adapter\Driver\Mysqli\Mysqli': $statementPrototype = new Driver\Mysqli\ProfilingStatement($this->options['buffer_results']); break; 

3) ZendDeveloperTools displays the number of requests, but does not list them. To list at the bottom of the page, I changed view / zend-developer-tools / toolbar / toolbar.phtml as follows:

 <!-- END Zend Developer Toolbar --> <?php $queryProfiles = $this->getHelperPluginManager()->getServiceLocator() ->get('Zend\Db\Adapter\Adapter')->getProfiler()->getQueryProfiles(); echo '<ol>'; foreach($queryProfiles as $queryObj) { $query = $queryObj->toArray(); echo '<li>'; echo '<b>' . ($query['elapsed']*1000) . '</b> ms<br/>'; echo $query['sql']; if(count($query['parameters'])) { echo '<br/><i>Parameters:</i> '; $list = array(); foreach($query['parameters'] as $key => $value) $list[] = '?'. $this->escapeHtml($key) ."='". $this->escapeHtml($value) ."'"; echo implode(', ', $list); } echo '</li>'; } echo '</ol>'; 
+5
source

In my case, I just looked at this in one in a try catch: $ e → __ toString () was the key

 try { this->tableGateway->insert($data); } catch (\Exception $e) { \Zend\Debug\Debug::dump($e->__toString()); exit; } 
+3
source

Source: https://habr.com/ru/post/1444637/


All Articles