‪TYPO3CMS  11.5
AbstractDatabaseRecordProvider.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
25 
29 abstract class ‪AbstractDatabaseRecordProvider implements LoggerAwareInterface
30 {
31  use LoggerAwareTrait;
32 
43  protected function ‪getRecordFromDatabase($tableName, $uid)
44  {
45  if ($uid <= 0) {
46  throw new \InvalidArgumentException(
47  '$uid must be positive integer, ' . $uid . ' given',
48  1437656456
49  );
50  }
51  $row = $this->‪getDatabaseRow($tableName, $uid);
52  if (empty($row)) {
53  // Indicates a runtime error (eg. record was killed by other editor meanwhile) can be caught elsewhere
54  // and transformed to a message to the user or something
56  'Record with uid ' . $uid . ' from table ' . $tableName . ' not found',
57  1437656081,
58  null,
59  $tableName,
60  (int)$uid
61  );
62  }
63  return $row;
64  }
65 
73  protected function ‪getDatabaseRow(string $tableName, int $uid): array
74  {
75  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
76  $queryBuilder->getRestrictions()
77  ->removeAll()
78  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
79 
80  $row = $queryBuilder->select('*')
81  ->from($tableName)
82  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT)))
83  ->executeQuery()
84  ->fetchAssociative();
85 
86  return $row ?: [];
87  }
88 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider
Definition: AbstractDatabaseRecordProvider.php:30
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider\getRecordFromDatabase
‪array getRecordFromDatabase($tableName, $uid)
Definition: AbstractDatabaseRecordProvider.php:43
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException
Definition: DatabaseRecordException.php:24
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider\getDatabaseRow
‪array getDatabaseRow(string $tableName, int $uid)
Definition: AbstractDatabaseRecordProvider.php:73