TYPO3 CMS  TYPO3_7-6
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 
78  public function add($object)
79  {
80  if (!$object instanceof $this->objectType) {
81  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
82  }
83  $this->persistenceManager->add($object);
84  }
85 
94  public function remove($object)
95  {
96  if (!$object instanceof $this->objectType) {
97  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
98  }
99  $this->persistenceManager->remove($object);
100  }
101 
111  public function update($modifiedObject)
112  {
113  if (!$modifiedObject instanceof $this->objectType) {
114  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);
115  }
116  $this->persistenceManager->update($modifiedObject);
117  }
118 
125  public function findAll()
126  {
127  return $this->createQuery()->execute();
128  }
129 
136  public function countAll()
137  {
138  return $this->createQuery()->execute()->count();
139  }
140 
148  public function removeAll()
149  {
150  foreach ($this->findAll() as $object) {
151  $this->remove($object);
152  }
153  }
154 
162  public function findByUid($uid)
163  {
164  return $this->findByIdentifier($uid);
165  }
166 
174  public function findByIdentifier($identifier)
175  {
176  return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
177  }
178 
191  public function setDefaultOrderings(array $defaultOrderings)
192  {
193  $this->defaultOrderings = $defaultOrderings;
194  }
195 
203  public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
204  {
205  $this->defaultQuerySettings = $defaultQuerySettings;
206  }
207 
214  public function createQuery()
215  {
216  $query = $this->persistenceManager->createQueryForType($this->objectType);
217  if ($this->defaultOrderings !== []) {
218  $query->setOrderings($this->defaultOrderings);
219  }
220  if ($this->defaultQuerySettings !== null) {
221  $query->setQuerySettings(clone $this->defaultQuerySettings);
222  }
223  return $query;
224  }
225 
235  public function __call($methodName, $arguments)
236  {
237  if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
238  $propertyName = lcfirst(substr($methodName, 6));
239  $query = $this->createQuery();
240  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
241  return $result;
242  } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
243  $propertyName = lcfirst(substr($methodName, 9));
244  $query = $this->createQuery();
245 
246  $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
247  if ($result instanceof QueryResultInterface) {
248  return $result->getFirst();
249  } elseif (is_array($result)) {
250  return isset($result[0]) ? $result[0] : null;
251  }
252  } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
253  $propertyName = lcfirst(substr($methodName, 7));
254  $query = $this->createQuery();
255  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
256  return $result;
257  }
258  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
259  }
260 
266  protected function getRepositoryClassName()
267  {
268  return get_class($this);
269  }
270 }
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:191
setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:203
$uid
Definition: server.php:38