‪TYPO3CMS  9.5
Repository.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
27  protected ‪$persistenceManager;
28 
32  protected ‪$objectManager;
33 
37  protected ‪$objectType;
38 
42  protected ‪$defaultOrderings = [];
43 
48 
53  {
54  $this->persistenceManager = ‪$persistenceManager;
55  }
56 
62  public function ‪__construct(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
63  {
64  $this->objectManager = ‪$objectManager;
66  }
67 
74  public function ‪add($object)
75  {
76  if (!$object instanceof $this->objectType) {
77  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
78  }
79  $this->persistenceManager->add($object);
80  }
81 
88  public function remove($object)
89  {
90  if (!$object instanceof $this->objectType) {
91  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
92  }
93  $this->persistenceManager->remove($object);
94  }
95 
103  public function ‪update($modifiedObject)
104  {
105  if (!$modifiedObject instanceof $this->objectType) {
106  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
107  }
108  $this->persistenceManager->update($modifiedObject);
109  }
110 
116  public function ‪findAll()
117  {
118  return $this->‪createQuery()->‪execute();
119  }
120 
126  public function ‪countAll()
127  {
128  return $this->‪createQuery()->‪execute()->count();
129  }
130 
135  public function ‪removeAll()
136  {
137  foreach ($this->‪findAll() as $object) {
138  $this->remove($object);
139  }
140  }
141 
148  public function ‪findByUid($uid)
149  {
150  return $this->‪findByIdentifier($uid);
151  }
152 
159  public function ‪findByIdentifier($identifier)
160  {
161  return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
162  }
163 
174  public function ‪setDefaultOrderings(array ‪$defaultOrderings)
175  {
176  $this->defaultOrderings = ‪$defaultOrderings;
177  }
178 
184  public function ‪setDefaultQuerySettings(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\QuerySettingsInterface ‪$defaultQuerySettings)
185  {
186  $this->defaultQuerySettings = ‪$defaultQuerySettings;
187  }
188 
194  public function ‪createQuery()
195  {
196  $query = $this->persistenceManager->createQueryForType($this->objectType);
197  if ($this->defaultOrderings !== []) {
198  $query->‪setOrderings($this->defaultOrderings);
199  }
200  if ($this->defaultQuerySettings !== null) {
201  $query->‪setQuerySettings(clone $this->defaultQuerySettings);
202  }
203  return $query;
204  }
205 
214  public function ‪__call($methodName, $arguments)
215  {
216  if (strpos($methodName, 'findBy') === 0 && strlen($methodName) > 7) {
217  $propertyName = lcfirst(substr($methodName, 6));
218  $query = $this->‪createQuery();
219  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
220  return $result;
221  }
222  if (strpos($methodName, 'findOneBy') === 0 && strlen($methodName) > 10) {
223  $propertyName = lcfirst(substr($methodName, 9));
224  $query = $this->‪createQuery();
225 
226  $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
227  if ($result instanceof QueryResultInterface) {
228  return $result->getFirst();
229  }
230  if (is_array($result)) {
231  return $result[0] ?? null;
232  }
233  } elseif (strpos($methodName, 'countBy') === 0 && strlen($methodName) > 8) {
234  $propertyName = lcfirst(substr($methodName, 7));
235  $query = $this->‪createQuery();
236  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
237  return $result;
238  }
239  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
240  }
241 
247  protected function ‪getRepositoryClassName()
248  {
249  return static::class;
250  }
251 }
‪TYPO3\CMS\Extbase\Persistence\Repository\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Repository.php:30
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:21
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Persistence\Repository\findByIdentifier
‪object findByIdentifier($identifier)
Definition: Repository.php:154
‪TYPO3
‪TYPO3\CMS\Extbase\Persistence\Repository\injectPersistenceManager
‪injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: Repository.php:47
‪TYPO3\CMS\Extbase\Persistence\Repository\add
‪add($object)
Definition: Repository.php:69
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setOrderings
‪TYPO3 CMS Extbase Persistence QueryInterface setOrderings(array $orderings)
‪TYPO3\CMS\Extbase\Persistence
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultOrderings
‪array $defaultOrderings
Definition: Repository.php:38
‪TYPO3\CMS\Extbase\Persistence\Repository\__construct
‪__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Repository.php:57
‪TYPO3\CMS\Extbase\Persistence\Repository\$objectType
‪string $objectType
Definition: Repository.php:34
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:189
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:179
‪TYPO3\CMS\Core\Utility\ClassNamingUtility
Definition: ClassNamingUtility.php:23
‪TYPO3\CMS\Core\Utility\ClassNamingUtility\translateRepositoryNameToModelName
‪static string translateRepositoryNameToModelName($repositoryName)
Definition: ClassNamingUtility.php:66
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultQuerySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $defaultQuerySettings
Definition: Repository.php:42
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:23
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setQuerySettings
‪setQuerySettings(Generic\QuerySettingsInterface $querySettings)
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Repository\getRepositoryClassName
‪string getRepositoryClassName()
Definition: Repository.php:242
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultOrderings
‪setDefaultOrderings(array $defaultOrderings)
Definition: Repository.php:169
‪TYPO3\CMS\Extbase\Persistence\Repository\findByUid
‪object findByUid($uid)
Definition: Repository.php:143
‪TYPO3\CMS\Extbase\Persistence\Repository\removeAll
‪removeAll()
Definition: Repository.php:130
‪TYPO3\CMS\Extbase\Persistence\Repository\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: Repository.php:26
‪TYPO3\CMS\Extbase\Persistence\Repository\__call
‪mixed __call($methodName, $arguments)
Definition: Repository.php:209
‪TYPO3\CMS\Extbase\Persistence\Repository\countAll
‪int countAll()
Definition: Repository.php:121
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\update
‪update($modifiedObject)
Definition: Repository.php:98
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\execute
‪TYPO3 CMS Extbase Persistence QueryResultInterface array execute($returnRawQueryResult=false)
‪TYPO3\CMS\Extbase\Persistence\Repository\findAll
‪QueryResultInterface array findAll()
Definition: Repository.php:111