doctrine-dbal example

April 20, 2018 at 18:10

For this blog I needed the 10 most used tags (tag) with a count of the entries (entry) assigned to each tag. I used the chance to make myself familiar with doctrine-dbal, since with the upcoming TYPO3 9 the good old TYPO3_DB will no longer be available. 

First the definition of the classes we're going to need:

use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \TYPO3\CMS\Core\Database\ConnectionPool;

Then there is the function findMostUsed, which fetches the X most used tags, including their entrycount: 

public function findMostUsed($limit){
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_ophiblog_domain_model_tag');
    $queryBuilder->select('tx_ophiblog_domain_model_tag.uid', '')
            $queryBuilder->expr()->count('tx_ophiblog_domain_model_tag.uid', 'count')
        ->leftJoin('tx_ophiblog_domain_model_tag', 'tx_ophiblog_entry_tag_mm', 'm',
        ->orderBy('count', 'desc');

    $statement = $queryBuilder->execute();
    $data = array();
    while($row = $statement->fetch()){
        $data[] = $row;
    return $data;

I'm still unsure if the iteration over $statement is truly necessary, but I'll see. So far it worked for me.

Whoever else has problems migrating TYPO3_DB, best start with this page. Its only covering the basics, but it's a start. 

Tags: Querybuilder TYPO3 8 group_by

Hinterlasse einen Kommentar: