* @package Extbase
* @subpackage Persistence
* @version $Id: Manager.php 2293 2009-05-20 18:14:45Z robert $
- *
+ * @api
*/
class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerInterface, t3lib_Singleton {
*/
protected $session;
+ /**
+ * @var Tx_Extbase_Object_ManagerInterface
+ */
+ protected $objectManager;
+
+ /**
+ * This is an array of registered repository class names.
+ *
+ * @var array
+ */
+ protected $repositoryClassNames = array();
+
/**
* Injects the Persistence Backend
*
* @param Tx_Extbase_Persistence_BackendInterface $backend The persistence backend
* @return void
-
*/
public function injectBackend(Tx_Extbase_Persistence_BackendInterface $backend) {
$this->backend = $backend;
*
* @param Tx_Extbase_Persistence_Session $session The persistence session
* @return void
-
*/
public function injectSession(Tx_Extbase_Persistence_Session $session) {
$this->session = $session;
}
+ /**
+ * Injects the object manager
+ *
+ * @param Tx_Extbase_Object_ManagerInterface $objectManager
+ * @return void
+ */
+ public function injectObjectManager(Tx_Extbase_Object_ManagerInterface $objectManager) {
+ $this->objectManager = $objectManager;
+ }
+
/**
* Returns the current persistence session
*
* @return Tx_Extbase_Persistence_Session
-
*/
public function getSession() {
return $this->session;
return $this->backend;
}
+ /**
+ * Registers a repository
+ *
+ * @param string $className The class name of the repository to be reigistered
+ * @return void
+ */
+ public function registerRepositoryClassName($className) {
+ $this->repositoryClassNames[] = $className;
+ }
+
+ /**
+ * Returns all repository class names
+ *
+ * @return array An array holding the registered repository class names
+ */
+ public function getRepositoryClassNames() {
+ return $this->repositoryClassNames;
+ }
+
/**
* Commits new objects and changes to objects in the current persistence
* session into the backend
*/
public function persistAll() {
$aggregateRootObjects = new Tx_Extbase_Persistence_ObjectStorage();
- $aggregateRootObjects->addAll($this->session->getAddedObjects());
- $aggregateRootObjects->addAll($this->session->getReconstitutedObjects());
+ $removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
- $removedObjects = $this->session->getRemovedObjects();
+ // fetch and inspect objects from all known repositories
+ $repositoryClassNames = $this->getRepositoryClassNames();
+ foreach ($repositoryClassNames as $repositoryClassName) {
+ $repository = $this->objectManager->getObject($repositoryClassName);
+ $aggregateRootObjects->addAll($repository->getAddedObjects());
+ $removedObjects->addAll($repository->getRemovedObjects());
+ }
+ foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
+ if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
+ $aggregateRootObjects->attach($reconstitutedObject);
+ }
+ }
+
+ // hand in only aggregate roots, leaving handling of subobjects to
+ // the underlying storage layer
$this->backend->setAggregateRootObjects($aggregateRootObjects);
$this->backend->setDeletedObjects($removedObjects);
$this->backend->commit();
+
+ // this needs to unregister more than just those, as at least some of
+ // the subobjects are supposed to go away as well...
+ // OTOH those do no harm, changes to the unused ones should not happen,
+ // so all they do is eat some memory.
+ foreach($removedObjects as $removedObject) {
+ $this->session->unregisterReconstitutedObject($removedObject);
+ }
}
+
}
?>
\ No newline at end of file