‪TYPO3CMS  ‪main
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;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 
30 abstract class ‪AbstractDatabaseRecordProvider implements LoggerAwareInterface
31 {
32  use LoggerAwareTrait;
33 
44  protected function ‪getRecordFromDatabase($tableName, ‪$uid)
45  {
46  if (‪$uid <= 0) {
47  throw new \InvalidArgumentException(
48  '$uid must be positive integer, ' . ‪$uid . ' given',
49  1437656456
50  );
51  }
52  $row = $this->‪getDatabaseRow($tableName, ‪$uid);
53  if (empty($row)) {
54  // Indicates a runtime error (eg. record was killed by other editor meanwhile) can be caught elsewhere
55  // and transformed to a message to the user or something
57  'Record with uid ' . ‪$uid . ' from table ' . $tableName . ' not found',
58  1437656081,
59  null,
60  $tableName,
61  (int)‪$uid
62  );
63  }
64  return BackendUtility::convertDatabaseRowValuesToPhp($tableName, $row);
65  }
66 
70  protected function ‪getDatabaseRow(string $tableName, int ‪$uid): array
71  {
72  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
73  $queryBuilder->getRestrictions()
74  ->removeAll()
75  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
76 
77  $row = $queryBuilder->select('*')
78  ->from($tableName)
79  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT)))
80  ->executeQuery()
81  ->fetchAssociative();
82 
83  return $row ?: [];
84  }
85 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider
Definition: AbstractDatabaseRecordProvider.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider\getRecordFromDatabase
‪array getRecordFromDatabase($tableName, $uid)
Definition: AbstractDatabaseRecordProvider.php:44
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException
Definition: DatabaseRecordException.php:24
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider\getDatabaseRow
‪getDatabaseRow(string $tableName, int $uid)
Definition: AbstractDatabaseRecordProvider.php:70
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52