[+FEATURE] Extbase (Persistence): Default orderings & QuerySettings
authorBastian Waidelich <bastian@typo3.org>
Tue, 16 Nov 2010 10:34:14 +0000 (10:34 +0000)
committerBastian Waidelich <bastian@typo3.org>
Tue, 16 Nov 2010 10:34:14 +0000 (10:34 +0000)
It is now possible to change the default orderings of a repository without needing to
modify the query by setting the $defaultOrderings field of your Repository to a
non-empty array:
protected $defaultOrderings = array(
  'title' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
  'date' => 'title' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
);
This will change the default ordering for all queries created by this repository. Of course
you can override the ordering by calling $query->setOrderings() in your custom finder method.

Besides it's now possible to change the default query settings of a repository. This
way you could for instance disable "respect storage pid" for all queries. We added a life-cycle
method "initializeObject" to the repository which will be executed as soon as the repository
is created. Just override it like:
public function initializeObject() {
  $querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
  $querySettings->setRespectStoragePage(FALSE);
  $this->setDefaultQuerySettings($querySettings);
}
Of course, QuerySettings can be overridden too in your custom finder method by calling $query->setQuerySettings();

This resolves: #10319

typo3/sysext/extbase/Classes/Persistence/Repository.php
typo3/sysext/extbase/Classes/Persistence/RepositoryInterface.php

index f26752b..e98a359 100644 (file)
@@ -65,11 +65,26 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
        protected $persistenceManager;
 
        /**
+        * @var Tx_Extbase_Object_ObjectManagerInterface
+        */
+       protected $objectManager;
+
+       /**
         * @var string
         */
        protected $objectType;
 
        /**
+        * @var array
+        */
+       protected $defaultOrderings = array();
+
+       /**
+        * @var Tx_Extbase_Persistence_QuerySettingsInterface
+        */
+       protected $defaultQuerySettings = NULL;
+
+       /**
         * Constructs a new Repository
         *
         * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
@@ -82,11 +97,22 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
                if ($objectManager === NULL) {
                        // Legacy creation, in case the object manager is NOT injected
                        // If ObjectManager IS there, then all properties are automatically injected
-                       $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
-                       $this->injectIdentityMap($objectManager->get('Tx_Extbase_Persistence_IdentityMap'));
-                       $this->injectQueryFactory($objectManager->get('Tx_Extbase_Persistence_QueryFactory'));
-                       $this->injectPersistenceManager($objectManager->get('Tx_Extbase_Persistence_Manager'));
+                       $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
+                       $this->injectIdentityMap($this->objectManager->get('Tx_Extbase_Persistence_IdentityMap'));
+                       $this->injectQueryFactory($this->objectManager->get('Tx_Extbase_Persistence_QueryFactory'));
+                       $this->injectPersistenceManager($this->objectManager->get('Tx_Extbase_Persistence_Manager'));
+               } else {
+                       $this->objectManager = $objectManager;
                }
+               $this->initializeObject();
+       }
+
+       /**
+        * Life cycle method. You can override this in your own repository
+        *
+        * @return void
+        */
+       public function initializeObject() {
        }
 
        /**
@@ -154,7 +180,7 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
         *
         * @param object $existingObject The existing object
         * @param object $newObject The new object
-        * return void
+        * @return void
         * @api
         */
        public function replace($existingObject, $newObject) {
@@ -289,13 +315,47 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
        }
 
        /**
+        * Sets the property names to order the result by per default.
+        * Expected like this:
+        * array(
+        *  'foo' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
+        *  'bar' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
+        * )
+        *
+        * @param array $defaultOrderings The property names to order by
+        * @return void
+        * @api
+        */
+       public function setDefaultOrderings(array $defaultOrderings) {
+               $this->defaultOrderings = $defaultOrderings;
+       }
+
+       /**
+        * Sets the default query settings to be used in this repository
+        *
+        * @param Tx_Extbase_Persistence_QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
+        * @return void
+        * @api
+        */
+       public function setDefaultQuerySettings(Tx_Extbase_Persistence_QuerySettingsInterface $defaultQuerySettings) {
+               $this->defaultQuerySettings = $defaultQuerySettings;
+       }
+
+       /**
         * Returns a query for objects of this repository
         *
         * @return Tx_Extbase_Persistence_QueryInterface
         * @api
         */
        public function createQuery() {
-               return $this->queryFactory->create($this->objectType);
+               $query = $this->queryFactory->create($this->objectType);
+               if ($this->defaultOrderings !== array()) {
+                       $query->setOrderings($this->defaultOrderings);
+               }
+               if ($this->defaultQuerySettings !== NULL) {
+                       $query->setQuerySettings($this->defaultQuerySettings);
+               }
+               return $query;
        }
 
        /**
index ca5c29c..2d8af1f 100644 (file)
@@ -54,6 +54,24 @@ interface Tx_Extbase_Persistence_RepositoryInterface {
        public function remove($object);
 
        /**
+        * Replaces an object by another.
+        *
+        * @param object $existingObject The existing object
+        * @param object $newObject The new object
+        * @return void
+        * @api
+        */
+       public function replace($existingObject, $newObject);
+
+       /**
+        * Replaces an existing object with the same identifier by the given object
+        *
+        * @param object $modifiedObject The modified object
+        * @api
+        */
+       public function update($modifiedObject);
+
+       /**
         * Returns all objects of this repository add()ed but not yet persisted to
         * the storage layer.
         *
@@ -78,6 +96,23 @@ interface Tx_Extbase_Persistence_RepositoryInterface {
        public function findAll();
 
        /**
+        * Returns the total number objects of this repository.
+        *
+        * @return integer The object count
+        * @api
+        */
+       public function countAll();
+
+       /**
+        * Removes all objects of this repository as if remove() was called for
+        * all of them.
+        *
+        * @return void
+        * @api
+        */
+       public function removeAll();
+
+       /**
         * Finds an object matching the given identifier.
         *
         * @param int $uid The identifier of the object to find
@@ -86,5 +121,36 @@ interface Tx_Extbase_Persistence_RepositoryInterface {
         */
        public function findByUid($uid);
 
+       /**
+        * Sets the property names to order the result by per default.
+        * Expected like this:
+        * array(
+        *  'foo' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
+        *  'bar' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
+        * )
+        *
+        * @param array $defaultOrderings The property names to order by
+        * @return void
+        * @api
+        */
+       public function setDefaultOrderings(array $defaultOrderings);
+
+       /**
+        * Sets the default query settings to be used in this repository
+        *
+        * @param Tx_Extbase_Persistence_QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
+        * @return void
+        * @api
+        */
+       public function setDefaultQuerySettings(Tx_Extbase_Persistence_QuerySettingsInterface $defaultQuerySettings);
+
+       /**
+        * Returns a query for objects of this repository
+        *
+        * @return Tx_Extbase_Persistence_QueryInterface
+        * @api
+        */
+       public function createQuery();
+
 }
 ?>
\ No newline at end of file