Shopware: Debug SQL Queries on the CLI
Fabian Blechschmidt
You are trying to debug a Command, ScheduledTask or Message on the queue?
Symfony Profiler and toolbar
For the frontend it is easy, you can just use the Symfony Profiler – easily activated by turning on the dev mode.
This can be done, by changing APP_ENV="prod" to APP_ENV="dev" in your .env file.
And then you have a toolbar in the footer which opens this:
SQL Queries on the CLI
But for the CLI it is not so easy. It took me a while to figure it out, but with this post from Alex on stackoverflow I got the right hint.
I have the problem, that @Alex doesn’t work for me, because on dev, Shopware is interfering at two positions:
src/Kernel.php:49
vendor/shopware/core/HttpKernel.php:179
So I just hacked the core:
\Shopware\Core\Profiling\Doctrine\DebugStack::startQuery
And added the few lines directly into DebugStack:
public function startQuery($sql, ?array $params = null, ?array $types = null): void
{
$this->ensureMasterSlaveCompatibility($sql);
// ADDED
$doctrineExtension = new \Shopware\Core\Profiling\Twig\DoctrineExtension();
echo $doctrineExtension->replaceQueryParameters(
$sql,
array_merge($params ?? array())
) . ';' . PHP_EOL;
// ADDED END
parent::startQuery($sql, $params, $types);
}
But don’t try this with the frontend ? Or you end up with this:
Other articles from this category
Shopware 6 Hidden Gems #16: CloneBehavior — entity duplication with overrides, one call
You know the admin’s „duplicate“ button on products. Now the ticket says: „We need that, but from code — duplicate a product per sales channel, with adjusted name and its own product number.“ Or: „Campaign landing pages: clone this CMS page 20 times with different headlines.“ I’ve watched this get implemented as: load entity with […]
Shopware 6 Hidden Gems #15: ChangeSet — before/after values for every write, no second query
The requirement sounds harmless every time: „Log when a product price changes, with old and new value.“ Or: „When an order’s email changes, notify the old address.“ Or the audit-trail classic: „Who changed what, from what, to what?“ And every time, the naive implementation is the same sad shape: subscribe to product.written, load the entity […]
Shopware 6 Hidden Gems #14: WriteProtected & ApiAware — field-level security without a single subscriber
A code review scenario. A plugin adds a purchasePrice-style field to a custom entity — internal margin data. The review question: „Can the Store API read this?“ Followed by: „Can the Admin API write that computed field you recalculate in a subscriber?“ The answers, in the plugin at hand, were „yes“ and „yes, and then […]