TYPO3 CMS  TYPO3_8-7
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 
25 {
30 
34  protected $objectManager;
35 
39  protected $objectType;
40 
44  protected $defaultOrderings = [];
45 
49  protected $defaultQuerySettings = null;
50 
55  {
56  $this->persistenceManager = $persistenceManager;
57  }
58 
64  public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
65  {
66  $this->objectManager = $objectManager;
68  }
69 
77  public function add($object)
78  {
79  if (!$object instanceof $this->objectType) {
80  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
81  }
82  $this->persistenceManager->add($object);
83  }
84 
92  public function remove($object)
93  {
94  if (!$object instanceof $this->objectType) {
95  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
96  }
97  $this->persistenceManager->remove($object);
98  }
99 
108  public function update($modifiedObject)
109  {
110  if (!$modifiedObject instanceof $this->objectType) {
111  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);
112  }
113  $this->persistenceManager->update($modifiedObject);
114  }
115 
122  public function findAll()
123  {
124  return $this->createQuery()->execute();
125  }
126 
133  public function countAll()
134  {
135  return $this->createQuery()->execute()->count();
136  }
137 
144  public function removeAll()
145  {
146  foreach ($this->findAll() as $object) {
147  $this->remove($object);
148  }
149  }
150 
158  public function findByUid($uid)
159  {
160  return $this->findByIdentifier($uid);
161  }
162 
170  public function findByIdentifier($identifier)
171  {
172  return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
173  }
174 
186  public function setDefaultOrderings(array $defaultOrderings)
187  {
188  $this->defaultOrderings = $defaultOrderings;
189  }
190 
197  public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
198  {
199  $this->defaultQuerySettings = $defaultQuerySettings;
200  }
201 
208  public function createQuery()
209  {
210  $query = $this->persistenceManager->createQueryForType($this->objectType);
211  if ($this->defaultOrderings !== []) {
212  $query->setOrderings($this->defaultOrderings);
213  }
214  if ($this->defaultQuerySettings !== null) {
215  $query->setQuerySettings(clone $this->defaultQuerySettings);
216  }
217  return $query;
218  }
219 
229  public function __call($methodName, $arguments)
230  {
231  if (substr($methodName, 0, 6) === 'findBy' && 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 (substr($methodName, 0, 9) === 'findOneBy' && 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 isset($result[0]) ? $result[0] : null;
247  }
248  } elseif (substr($methodName, 0, 7) === 'countBy' && 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 \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
255  }
256 
262  protected function getRepositoryClassName()
263  {
264  return get_class($this);
265  }
266 }
static translateRepositoryNameToModelName($repositoryName)
__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Repository.php:64
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: Repository.php:54
setDefaultOrderings(array $defaultOrderings)
Definition: Repository.php:186
setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:197