How to query the database directly without the ORM layer in Neos Flow

Neos Flows default persistence layer is based on Doctrine ORM which is super great for DDD and modelling your data!

The ORM brings some great mechanism in regards of annotations and migration creation. And to do this in high speed, there is a number of calculations and objects creation to go through with each query.

But, sometimes you wan’t to perform a old school regular SELECT COUNT(*) FROM table query, for a statistic or report. With ORM and the default Query object from your repository you can do something like

public function countAllForStatistic(): int
{
    $query = $this->createQuery();
    return $query->count()
}

This is great, but will start the whole ORM layer to create the query.

Access the database directly

But, since we are build on top of Doctrine ORM – which again is based on Doctrine DBAL – we can access the connection to the database directly.

With Neos Flow 7 came a connection factory that we can inject as such

<?php

namespace Vendor\Application\Service;

use Neos\Flow\Annotations as Flow;
use Doctrine\DBAL\Connection;

class StatisticService
{
    /**
     * @Flow\Inject
     * @var Connection
     */
    protected $databaseConnection;

    public function countNewSinceDay(): int
    {
        return $this->databaseConnection->executeQuery("SELECT COUNT(*) FROM table");
    }
}

Example package available

I created a example example package to show how this works (described in the pull request linked above)

https://github.com/sorenmalling/Vendor.TestConnectionFactory

Leave a Reply

Your email address will not be published. Required fields are marked *