Home > Back-end >  what is the difference with these query classes in TYPO3?
what is the difference with these query classes in TYPO3?

Time:11-23

I'm using extbase in my extension and so I have *Repository classes where I can do simple queries just like:

   public function getRecordsByCondition($config = [],$recordPages = null) {

        $recordQuery = $this->createQuery();

        $constraints = [];
        if ($config['field1']) {
            $constraints[] = $recordQuery->equals('field1',$config['field1']));
        }
        if ($config['field2']) {
            $constraints[] = $recordQuery->equals('field2',$config['field2']));
        }
        if ($config['field3']) {
            $constraints[] = $recordQuery->equals('field3',$config['field3']));
        }
        if (count($constraints)) {
            if ($recordPages) {
                $constraints[] = $recordQuery->in('pid',$recordPages);
                $recordQuery->getQuerySettings()->setRespectStoragePage(false);
            }
            $recordQuery->matching($recordQuery->logicalAnd($constraints));
        } else {
            return false;
        }
        return $recordQuery->execute();
   }

this will respect enableFields and other usual conditions.

on the other hand there is the option to do it in this way:

   public function getrecords2($config,$recordPages) {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                        ->getQueryBuilderForTable('tx_myext_domain_model_records');

        $rawquery = $queryBuilder
                ->select('*')
                ->from('tx_myext_domain_model_records')
                ->where(
                     $queryBuilder->expr()->eq('field1',$config['field1']),
                     $queryBuilder->expr()->eq('field2',$config['field2']),
                     $queryBuilder->expr()->eq('field3',$config['field3']),
                     $queryBuilder->expr()->in('pid', $recordPages),
                     $queryBuilder->expr()->eq('deleted',0),
                     $queryBuilder->expr()->eq('hidden',0)
                     // starttime, endtime, language, workspace, ....
                );
        return $rawquery->execute()->fetchAll();
    }

where I need to care about enablefields by myself but have more options to specify the query.

On the first view you can see that there are other methods (eq vs. equals) and these kind of doing queries have no relation. But both work on the same table.

Now I'm at a point where I need to change all my work from first to second variant as I need a query with a join to another table which can't be done with first variant (as far as I know).
Have I missed something or does the first variant needs some enhancements?

CodePudding user response:

Well, I am not sure exactly the difference but let me try to express things in brief as per my knowledge :D

The main difference between both queries is Individual database queries (Typically I call it Extbase query, I'm not sure I am right or not!) and another is Doctrine DBAL Queries

1. Individual database queries

Here, as per the modern approach extension use Domain modeling. So, TYPO3 already enables a secure connection for model (Typically database table) and you can use relational table connection with Extbase function (Select, operational, join etc..) provided by TYPO3 core.

For more: https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/6-Persistence/3-implement-individual-database-queries.html

2. Doctrine DBAL

Here, you enable connection manually for the database table using ConnectionPool class. Also, you have more feasibility to establish a relation (or Join you can say!) according to your need.

For more: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/Index.html

However, you can use restriction for taking care if hidden delete etc.

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                        ->getQueryBuilderForTable('tx_myext_domain_model_records');

$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
$rawquery = $queryBuilder
                ->select('*')
                ->from('tx_myext_domain_model_records')
                ->where(
                     $queryBuilder->expr()->eq('field1',$config['field1']),
                     $queryBuilder->expr()->eq('field2',$config['field2']),
                     $queryBuilder->expr()->eq('field3',$config['field3']),
                     $queryBuilder->expr()->in('pid', $recordPages)
                     // starttime, endtime, language, workspace, ....
                );

See: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/RestrictionBuilder/Index.html

I know this is not a sufficient and 100% correct answer. Everyone can welcome to correct me :)

CodePudding user response:

where I need to care about enablefields by myself

That's not true. By default there are Restrictions active and you can enable or disable every Restriction with a short command.

I use both approaches, but I use the first one only on Extbase extensions, the second one on every other extension. (Yes, there exist extensions without Extbase)

  • Related