‪TYPO3CMS  10.4
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 
24 
29 {
33  protected ‪$persistenceManager;
34 
38  protected ‪$objectManager;
39 
43  protected ‪$objectType;
44 
48  protected ‪$defaultOrderings = [];
49 
57 
62  {
63  $this->persistenceManager = ‪$persistenceManager;
64  }
65 
72  {
73  $this->objectManager = ‪$objectManager;
75  }
76 
83  public function ‪add($object)
84  {
85  if (!$object instanceof $this->objectType) {
86  throw new IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
87  }
88  $this->persistenceManager->add($object);
89  }
90 
97  public function remove($object)
98  {
99  if (!$object instanceof $this->objectType) {
100  throw new IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
101  }
102  $this->persistenceManager->remove($object);
103  }
104 
112  public function ‪update($modifiedObject)
113  {
114  if (!$modifiedObject instanceof $this->objectType) {
115  throw new IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
116  }
117  $this->persistenceManager->update($modifiedObject);
118  }
119 
125  public function ‪findAll()
126  {
127  return $this->‪createQuery()->‪execute();
128  }
129 
135  public function ‪countAll()
136  {
137  return $this->‪createQuery()->‪execute()->count();
138  }
139 
144  public function ‪removeAll()
145  {
146  foreach ($this->‪findAll() as $object) {
147  $this->remove($object);
148  }
149  }
150 
157  public function ‪findByUid($uid)
158  {
159  return $this->‪findByIdentifier($uid);
160  }
161 
168  public function ‪findByIdentifier($identifier)
169  {
170  return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
171  }
172 
183  public function ‪setDefaultOrderings(array ‪$defaultOrderings)
184  {
185  $this->defaultOrderings = ‪$defaultOrderings;
186  }
187 
199  public function ‪setDefaultQuerySettings(QuerySettingsInterface ‪$defaultQuerySettings)
200  {
201  $this->defaultQuerySettings = ‪$defaultQuerySettings;
202  }
203 
209  public function ‪createQuery()
210  {
211  $query = $this->persistenceManager->createQueryForType($this->objectType);
212  if ($this->defaultOrderings !== []) {
213  $query->‪setOrderings($this->defaultOrderings);
214  }
215  if ($this->defaultQuerySettings !== null) {
216  $query->‪setQuerySettings(clone $this->defaultQuerySettings);
217  }
218  return $query;
219  }
220 
229  public function ‪__call($methodName, $arguments)
230  {
231  if (strpos($methodName, 'findBy') === 0 && strlen($methodName) > 7) {
232  $propertyName = lcfirst(substr($methodName, 6));
233  $query = $this->‪createQuery();
234  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
235  return $result;
236  }
237  if (strpos($methodName, 'findOneBy') === 0 && strlen($methodName) > 10) {
238  $propertyName = lcfirst(substr($methodName, 9));
239  $query = $this->‪createQuery();
240 
241  $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
242  if ($result instanceof QueryResultInterface) {
243  return $result->getFirst();
244  }
245  if (is_array($result)) {
246  return $result[0] ?? null;
247  }
248  } elseif (strpos($methodName, 'countBy') === 0 && strlen($methodName) > 8) {
249  $propertyName = lcfirst(substr($methodName, 7));
250  $query = $this->‪createQuery();
251  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
252  return $result;
253  }
254  throw new UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
255  }
256 
262  protected function ‪getRepositoryClassName()
263  {
264  return static::class;
265  }
266 }
‪TYPO3\CMS\Extbase\Persistence\Repository\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Repository.php:36
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\__construct
‪__construct(ObjectManagerInterface $objectManager)
Definition: Repository.php:66
‪TYPO3\CMS\Extbase\Persistence\Repository\add
‪add($object)
Definition: Repository.php:78
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setOrderings
‪TYPO3 CMS Extbase Persistence QueryInterface setOrderings(array $orderings)
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\execute
‪TYPO3 CMS Extbase Persistence QueryResultInterface object[] execute($returnRawQueryResult=false)
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setQuerySettings
‪setQuerySettings(QuerySettingsInterface $querySettings)
‪TYPO3\CMS\Extbase\Persistence\Repository\findByUid
‪object null findByUid($uid)
Definition: Repository.php:152
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
Definition: UnsupportedMethodException.php:26
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultOrderings
‪array $defaultOrderings
Definition: Repository.php:44
‪TYPO3\CMS\Extbase\Persistence\Repository\$objectType
‪string $objectType
Definition: Repository.php:40
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:204
‪TYPO3\CMS\Core\Utility\ClassNamingUtility
Definition: ClassNamingUtility.php:24
‪TYPO3\CMS\Core\Utility\ClassNamingUtility\translateRepositoryNameToModelName
‪static string translateRepositoryNameToModelName($repositoryName)
Definition: ClassNamingUtility.php:50
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultQuerySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $defaultQuerySettings
Definition: Repository.php:51
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:29
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\getRepositoryClassName
‪string getRepositoryClassName()
Definition: Repository.php:257
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:194
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultOrderings
‪setDefaultOrderings(array $defaultOrderings)
Definition: Repository.php:178
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\removeAll
‪removeAll()
Definition: Repository.php:139
‪TYPO3\CMS\Extbase\Persistence\Repository\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: Repository.php:32
‪TYPO3\CMS\Extbase\Persistence\Repository\findByIdentifier
‪object null findByIdentifier($identifier)
Definition: Repository.php:163
‪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:130
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Extbase\Persistence\Repository\update
‪update($modifiedObject)
Definition: Repository.php:107
‪TYPO3\CMS\Extbase\Persistence\Repository\findAll
‪QueryResultInterface array findAll()
Definition: Repository.php:120
‪TYPO3\CMS\Extbase\Persistence\Repository\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: Repository.php:56
‪TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
Definition: IllegalObjectTypeException.php:26