TYPO3 CMS  TYPO3_6-2
Repository.php
Go to the documentation of this file.
1 <?php
3 
22 
28  protected $identityMap;
29 
35  protected $backend;
36 
42  protected $session;
43 
49 
53  protected $objectManager;
54 
58  protected $objectType;
59 
63  protected $defaultOrderings = array();
64 
68  protected $defaultQuerySettings = NULL;
69 
75  public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
76  $this->objectManager = $objectManager;
77 
78  $nsSeparator = strpos($this->getRepositoryClassName(), '\\') !== FALSE ? '\\\\' : '_';
79  $this->objectType = preg_replace(array('/' . $nsSeparator . 'Repository' . $nsSeparator . '(?!.*' . $nsSeparator . 'Repository' . $nsSeparator . ')/', '/Repository$/'), array($nsSeparator . 'Model' . $nsSeparator, ''), $this->getRepositoryClassName());
80  }
81 
90  public function add($object) {
91  if (!$object instanceof $this->objectType) {
92  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
93  }
94  $this->persistenceManager->add($object);
95  }
96 
105  public function remove($object) {
106  if (!$object instanceof $this->objectType) {
107  throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
108  }
109  $this->persistenceManager->remove($object);
110  }
111 
119  public function replace($existingObject, $newObject) {
120  // Does nothing here as explicit update replaces objects in persistence session already
121  }
122 
132  public function update($modifiedObject) {
133  if (!$modifiedObject instanceof $this->objectType) {
134  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);
135  }
136  $this->persistenceManager->update($modifiedObject);
137  }
138 
145  public function findAll() {
146  return $this->createQuery()->execute();
147  }
148 
155  public function countAll() {
156  return $this->createQuery()->execute()->count();
157  }
158 
166  public function removeAll() {
167  foreach ($this->findAll() AS $object) {
168  $this->remove($object);
169  }
170  }
171 
179  public function findByUid($uid) {
180  return $this->findByIdentifier($uid);
181  }
182 
190  public function findByIdentifier($identifier) {
205  if ($this->session->hasIdentifier($identifier, $this->objectType)) {
206  $object = $this->session->getObjectByIdentifier($identifier, $this->objectType);
207  } else {
208  $query = $this->createQuery();
209  $query->getQuerySettings()->setRespectStoragePage(FALSE);
210  $query->getQuerySettings()->setRespectSysLanguage(FALSE);
211  $object = $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
212  }
213 
214  return $object;
215  }
216 
229  public function setDefaultOrderings(array $defaultOrderings) {
230  $this->defaultOrderings = $defaultOrderings;
231  }
232 
240  public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
241  $this->defaultQuerySettings = $defaultQuerySettings;
242  }
243 
250  public function createQuery() {
251  $query = $this->persistenceManager->createQueryForType($this->objectType);
252  if ($this->defaultOrderings !== array()) {
253  $query->setOrderings($this->defaultOrderings);
254  }
255  if ($this->defaultQuerySettings !== NULL) {
256  $query->setQuerySettings(clone $this->defaultQuerySettings);
257  }
258  return $query;
259  }
260 
270  public function __call($methodName, $arguments) {
271  if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
272  $propertyName = lcfirst(substr($methodName, 6));
273  $query = $this->createQuery();
274  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
275  return $result;
276  } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
277  $propertyName = lcfirst(substr($methodName, 9));
278  $query = $this->createQuery();
279 
280  $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
281  if ($result instanceof QueryResultInterface) {
282  return $result->getFirst();
283  } elseif (is_array($result)) {
284  return isset($result[0]) ? $result[0] : NULL;
285  }
286 
287  } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
288  $propertyName = lcfirst(substr($methodName, 7));
289  $query = $this->createQuery();
290  $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
291  return $result;
292  }
293  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
294  }
295 
301  protected function getRepositoryClassName() {
302  return get_class($this);
303  }
304 }
$uid
Definition: server.php:36
replace($existingObject, $newObject)
Definition: Repository.php:119
__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Repository.php:75
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
setDefaultOrderings(array $defaultOrderings)
Definition: Repository.php:229
setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:240