‪TYPO3CMS  ‪main
Repository.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 
23 
30 {
34  protected ‪$persistenceManager;
35 
40  protected ‪$objectType;
41 
45  protected $defaultOrderings = [];
46 
53  protected $defaultQuerySettings;
54 
55  public function injectPersistenceManager(‪PersistenceManagerInterface ‪$persistenceManager)
56  {
58  }
59 
63  public function ‪__construct()
64  {
66  }
67 
75  public function ‪add($object)
76  {
77  if (!$object instanceof $this->objectType) {
78  throw new ‪IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
79  }
80  $this->‪persistenceManager->add($object);
81  }
82 
90  public function remove($object)
91  {
92  if (!$object instanceof $this->objectType) {
93  throw new IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
94  }
95  $this->‪persistenceManager->remove($object);
96  }
97 
106  public function ‪update($modifiedObject)
107  {
108  if (!$modifiedObject instanceof $this->objectType) {
109  throw new IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
110  }
111  $this->‪persistenceManager->update($modifiedObject);
112  }
113 
120  public function ‪findAll()
121  {
122  return $this->‪createQuery()->‪execute();
123  }
124 
130  public function ‪countAll()
131  {
132  return $this->‪createQuery()->‪execute()->count();
133  }
134 
139  public function ‪removeAll()
140  {
141  foreach ($this->‪findAll() as $object) {
142  $this->remove($object);
143  }
144  }
145 
153  public function ‪findByUid(‪$uid)
154  {
155  return $this->‪findByIdentifier(‪$uid);
156  }
157 
165  public function ‪findByIdentifier(‪$identifier)
166  {
167  return $this->‪persistenceManager->getObjectByIdentifier(‪$identifier, $this->objectType);
168  }
169 
180  public function ‪setDefaultOrderings(array $defaultOrderings)
181  {
182  $this->defaultOrderings = $defaultOrderings;
183  }
184 
196  public function ‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
197  {
198  $this->defaultQuerySettings = $defaultQuerySettings;
199  }
200 
207  public function ‪createQuery()
208  {
209  $query = $this->‪persistenceManager->createQueryForType($this->objectType);
210  if ($this->defaultOrderings !== []) {
211  $query->setOrderings($this->defaultOrderings);
212  }
213  if ($this->defaultQuerySettings !== null) {
214  $query->setQuerySettings(clone $this->defaultQuerySettings);
215  }
216  return $query;
217  }
218 
228  public function ‪__call($methodName, $arguments)
229  {
230  if (str_starts_with($methodName, 'findBy') && strlen($methodName) > 7) {
231  trigger_error(
232  'Usage of magic method ' . static::class . '->findBy[Property]() is deprecated and will be removed in TYPO3 v14.0, use method findBy() instead.',
233  E_USER_DEPRECATED
234  );
235  $propertyName = lcfirst(substr($methodName, 6));
236  $query = $this->‪createQuery();
237  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
238  return $result;
239  }
240  if (str_starts_with($methodName, 'findOneBy') && strlen($methodName) > 10) {
241  trigger_error(
242  'Usage of magic method ' . static::class . '->findOneBy[Property]() is deprecated and will be removed in TYPO3 v14.0, use method findOneBy() instead.',
243  E_USER_DEPRECATED
244  );
245  $propertyName = lcfirst(substr($methodName, 9));
246  $query = $this->‪createQuery();
247 
248  $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
249  if ($result instanceof QueryResultInterface) {
250  return $result->getFirst();
251  }
252  if (is_array($result)) {
253  return $result[0] ?? null;
254  }
255  } elseif (str_starts_with($methodName, 'countBy') && strlen($methodName) > 8) {
256  trigger_error(
257  'Usage of magic method ' . static::class . '->countBy[Property]() is deprecated and will be removed in TYPO3 v14.0, use method count() instead.',
258  E_USER_DEPRECATED
259  );
260  $propertyName = lcfirst(substr($methodName, 7));
261  $query = $this->‪createQuery();
262  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
263  return $result;
264  }
265  throw new UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
266  }
267 
276  public function ‪findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): QueryResultInterface
277  {
278  $query = $this->‪createQuery();
279  $constraints = [];
280  foreach ($criteria as $propertyName => $propertyValue) {
281  $constraints[] = $query->equals($propertyName, $propertyValue);
282  }
283 
284  if (($numberOfConstraints = ‪count($constraints)) === 1) {
285  $query->matching(...$constraints);
286  } elseif ($numberOfConstraints > 1) {
287  $query->matching($query->logicalAnd(...$constraints));
288  }
289 
290  if (is_array($orderBy)) {
291  $query->setOrderings($orderBy);
292  }
293 
294  if (is_int($limit)) {
295  $query->setLimit($limit);
296  }
297 
298  if (is_int($offset)) {
299  $query->setOffset($offset);
300  }
301 
302  return $query->execute();
303  }
304 
310  public function ‪findOneBy(array $criteria, array $orderBy = null): object|null
311  {
312  return $this->‪findBy($criteria, $orderBy, 1)->‪getFirst();
313  }
314 
319  public function ‪count(array $criteria): int
320  {
321  return $this->‪findBy($criteria)->count();
322  }
323 
329  protected function ‪getRepositoryClassName()
330  {
331  return static::class;
332  }
333 }
‪TYPO3\CMS\Extbase\Persistence\Repository\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: Repository.php:33
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\Repository\findBy
‪QueryResultInterface findBy(array $criteria, array $orderBy=null, int $limit=null, int $offset=null)
Definition: Repository.php:272
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Core\Utility\ClassNamingUtility\translateRepositoryNameToModelName
‪static class string translateRepositoryNameToModelName(string $repositoryName)
Definition: ClassNamingUtility.php:52
‪TYPO3\CMS\Extbase\Persistence\Repository\add
‪add($object)
Definition: Repository.php:71
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface\getFirst
‪object null getFirst()
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\execute
‪TYPO3 CMS Extbase Persistence QueryResultInterface object[] execute($returnRawQueryResult=false)
‪TYPO3\CMS\Extbase\Persistence\Repository\findByUid
‪object null findByUid($uid)
Definition: Repository.php:149
‪TYPO3\CMS\Extbase\Persistence\Repository\persistenceManager
‪array< non-empty-string, $defaultOrderings=array();protected QuerySettingsInterface $defaultQuerySettings;public function injectPersistenceManager(PersistenceManagerInterface $persistenceManager) { $this-> persistenceManager
Definition: Repository.php:53
‪TYPO3\CMS\Extbase\Persistence\Repository\count
‪count(array $criteria)
Definition: Repository.php:315
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
Definition: UnsupportedMethodException.php:25
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\Repository\__construct
‪__construct()
Definition: Repository.php:59
‪TYPO3\CMS\Extbase\Persistence\Repository\$objectType
‪string $objectType
Definition: Repository.php:38
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:25
‪TYPO3\CMS\Core\Utility\ClassNamingUtility
Definition: ClassNamingUtility.php:28
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:30
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:192
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultOrderings
‪setDefaultOrderings(array $defaultOrderings)
Definition: Repository.php:176
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Repository\removeAll
‪removeAll()
Definition: Repository.php:135
‪TYPO3\CMS\Extbase\Persistence\Repository\getRepositoryClassName
‪class string< static > getRepositoryClassName()
Definition: Repository.php:325
‪TYPO3\CMS\Extbase\Persistence\Repository\findByIdentifier
‪object null findByIdentifier($identifier)
Definition: Repository.php:161
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Extbase\Persistence\Repository\__call
‪mixed __call($methodName, $arguments)
Definition: Repository.php:224
‪TYPO3\CMS\Extbase\Persistence\Repository\countAll
‪int countAll()
Definition: Repository.php:126
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\findOneBy
‪findOneBy(array $criteria, array $orderBy=null)
Definition: Repository.php:306
‪TYPO3\CMS\Extbase\Persistence\Repository\update
‪update($modifiedObject)
Definition: Repository.php:102
‪TYPO3\CMS\Extbase\Persistence\Repository\findAll
‪QueryResultInterface array findAll()
Definition: Repository.php:116
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪QueryInterface createQuery()
Definition: Repository.php:203
‪TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
Definition: IllegalObjectTypeException.php:25
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37