‪TYPO3CMS  ‪main
AbstractRepository.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 
27 
34 {
38  protected ‪$table = '';
39 
43  protected ‪$factory;
44 
48  protected ‪$typeField = '';
49 
53  protected ‪$type = '';
54 
60  protected ‪$objectType;
61 
65  public function ‪__construct()
66  {
67  $this->factory = GeneralUtility::makeInstance(ResourceFactory::class);
68  }
69 
76  public function ‪add($object)
77  {
78  }
79 
86  public function remove($object)
87  {
88  }
89 
98  public function ‪replace($existingObject, $newObject)
99  {
100  }
101 
108  public function ‪update($modifiedObject)
109  {
110  }
111 
120  public function ‪getAddedObjects()
121  {
122  return [];
123  }
124 
133  public function ‪getRemovedObjects()
134  {
135  return [];
136  }
137 
144  public function ‪findAll()
145  {
146  $items = [];
147  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
148  if ($this->‪getEnvironmentMode() === 'FE') {
149  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
150  }
151  $queryBuilder
152  ->select('*')
153  ->from($this->table);
154 
155  if (!empty($this->type)) {
156  $queryBuilder->where(
157  $queryBuilder->expr()->eq(
158  $this->typeField,
159  $queryBuilder->createNamedParameter($this->type)
160  )
161  );
162  }
163  $result = $queryBuilder->executeQuery();
164 
165  // fetch all records and create objects out of them
166  while ($row = $result->fetchAssociative()) {
167  $items[] = $this->‪createDomainObject($row);
168  }
169  return $items;
170  }
171 
179  abstract protected function ‪createDomainObject(array $databaseRow);
180 
186  public function ‪countAll()
187  {
188  return 0;
189  }
190 
195  public function ‪removeAll()
196  {
197  }
198 
209  public function ‪findByUid(‪$uid)
210  {
212  throw new \InvalidArgumentException('The UID has to be an integer. UID given: "' . ‪$uid . '"', 1316779798);
213  }
214  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
215  if ($this->‪getEnvironmentMode() === 'FE') {
216  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
217  }
218  $row = $queryBuilder
219  ->select('*')
220  ->from($this->table)
221  ->where(
222  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT))
223  )
224  ->executeQuery()
225  ->fetchAssociative();
226  if (!is_array($row)) {
227  throw new \RuntimeException('Could not find row with UID "' . ‪$uid . '" in table "' . $this->table . '"', 1314354065);
228  }
229  return $this->‪createDomainObject($row);
230  }
231 
244  public function ‪setDefaultOrderings(array $defaultOrderings)
245  {
246  throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1313185906);
247  }
248 
256  public function ‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
257  {
258  throw new \BadMethodCallException('Repository does not support the setDefaultQuerySettings() method.', 1313185907);
259  }
260 
267  public function ‪createQuery()
268  {
269  throw new \BadMethodCallException('Repository does not support the createQuery() method.', 1313185908);
270  }
271 
281  public function ‪findByIdentifier(‪$identifier)
282  {
283  return $this->‪findByUid(‪$identifier);
284  }
285 
295  public function ‪__call($method, $arguments)
296  {
297  throw new \BadMethodCallException('Repository method "' . $method . '" is not implemented.', 1378918410);
298  }
299 
305  public function ‪getEntityClassName()
306  {
307  return ‪$this->objectType;
308  }
309 
316  protected function ‪getEnvironmentMode()
317  {
318  return (‪$GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController ? 'FE' : 'BE';
319  }
320 }
‪TYPO3\CMS\Core\Resource\AbstractRepository\setDefaultOrderings
‪setDefaultOrderings(array $defaultOrderings)
Definition: AbstractRepository.php:239
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:46
‪TYPO3\CMS\Core\Resource\AbstractRepository\getAddedObjects
‪array getAddedObjects()
Definition: AbstractRepository.php:115
‪TYPO3\CMS\Core\Resource\AbstractRepository\$objectType
‪string $objectType
Definition: AbstractRepository.php:55
‪TYPO3\CMS\Core\Resource\AbstractRepository\update
‪update($modifiedObject)
Definition: AbstractRepository.php:103
‪TYPO3\CMS\Core\Resource\AbstractRepository\countAll
‪int countAll()
Definition: AbstractRepository.php:181
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:34
‪TYPO3\CMS\Core\Resource\AbstractRepository\$typeField
‪string $typeField
Definition: AbstractRepository.php:45
‪TYPO3\CMS\Core\Resource\AbstractRepository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: AbstractRepository.php:262
‪TYPO3\CMS\Core\Resource\AbstractRepository\$factory
‪ResourceFactory $factory
Definition: AbstractRepository.php:41
‪TYPO3\CMS\Core\Resource\AbstractRepository\__call
‪__call($method, $arguments)
Definition: AbstractRepository.php:290
‪TYPO3\CMS\Core\Resource\AbstractRepository\add
‪add($object)
Definition: AbstractRepository.php:71
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEntityClassName
‪string getEntityClassName()
Definition: AbstractRepository.php:300
‪TYPO3\CMS\Core\Resource\AbstractRepository\$table
‪string $table
Definition: AbstractRepository.php:37
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:25
‪TYPO3\CMS\Core\Resource\AbstractRepository\$type
‪string $type
Definition: AbstractRepository.php:49
‪TYPO3\CMS\Core\Resource\AbstractRepository\replace
‪replace($existingObject, $newObject)
Definition: AbstractRepository.php:93
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\AbstractRepository\findByUid
‪object findByUid($uid)
Definition: AbstractRepository.php:204
‪TYPO3\CMS\Core\Resource\AbstractRepository\findByIdentifier
‪object findByIdentifier($identifier)
Definition: AbstractRepository.php:276
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:24
‪TYPO3\CMS\Core\Resource\AbstractRepository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: AbstractRepository.php:251
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\AbstractRepository\removeAll
‪removeAll()
Definition: AbstractRepository.php:190
‪TYPO3\CMS\Core\Resource\AbstractRepository\getRemovedObjects
‪array getRemovedObjects()
Definition: AbstractRepository.php:128
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:35
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:102
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\AbstractRepository\findAll
‪array findAll()
Definition: AbstractRepository.php:139
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Resource\AbstractRepository\__construct
‪__construct()
Definition: AbstractRepository.php:60
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:30
‪TYPO3\CMS\Core\Resource\AbstractRepository\createDomainObject
‪object createDomainObject(array $databaseRow)
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: AbstractRepository.php:311
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37