ExtBase:
authorJochen Rau <j.rau@web.de>
Sat, 28 Mar 2009 09:47:44 +0000 (09:47 +0000)
committerJochen Rau <j.rau@web.de>
Sat, 28 Mar 2009 09:47:44 +0000 (09:47 +0000)
* Added several methods to check for common columns (crdate, cruser_id, tstamp etc.) and retrieve their names
* Implemented these checks in the ORM

typo3/sysext/extbase/Classes/DomainObject/AbstractDomainObject.php
typo3/sysext/extbase/Classes/MVC/Web/Request.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMap.php
typo3/sysext/extbase/Classes/Persistence/Mapper/ObjectRelationalMapper.php
typo3/sysext/extbase/Tests/Persistence/Mapper/DataMap_testcase.php
typo3/sysext/extbase/class.tx_extbase_dispatcher.php

index 398b86d..13bb892 100644 (file)
@@ -63,7 +63,6 @@ abstract class Tx_ExtBase_DomainObject_AbstractDomainObject implements Tx_ExtBas
                        $this->_reconstituteProperty($propertyName, $value);
                }
                $this->initializeObject();
-               $this->initializeCleanProperties();
        }
 
        /**
@@ -168,7 +167,7 @@ abstract class Tx_ExtBase_DomainObject_AbstractDomainObject implements Tx_ExtBas
         */
        private function initializeCleanProperties() {
                $properties = get_object_vars($this);
-               $dataMapper = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper');
+               $dataMapper = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper'); // singleton
                foreach ($properties as $propertyName => $propertyValue) {
                        if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
                                $this->_cleanProperties[$propertyName] = NULL;
index 2dbdc1a..4a6dc3f 100755 (executable)
@@ -32,7 +32,7 @@
  * @scope prototype
  */
 class Tx_ExtBase_MVC_Web_Request extends Tx_ExtBase_MVC_Request {
-
+// TODO Add a method to get a plugin id; useful to distinguish between several plugins on one page using the same controller
        /**
         * @var string The requested representation format
         */
index 887cf7a..374bc82 100644 (file)
@@ -103,13 +103,19 @@ class Tx_ExtBase_Persistence_Mapper_DataMap {
        protected function addCommonColumns() {
                $this->addColumn('uid', Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
                $this->addColumn('pid', Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
-               $this->addColumn('tstamp', Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_DATE);
-               $this->addColumn('crdate', Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_DATE);
-               $this->addColumn('cruser_id', Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
-               if ($this->getDeletedColumnName() !== NULL) {
+               if ($this->hasTimestampColumn()) {
+                       $this->addColumn($this->getTimestampColumnName(), Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               }
+               if ($this->hasCreationDateColumn()) {
+                       $this->addColumn($this->getCreationDateColumnName(), Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               }
+               if ($this->hasCreatorUidColumn()) {
+                       $this->addColumn($this->getCreatorUidColumnName(), Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
+               }
+               if ($this->hasDeletedColumn()) {
                        $this->addColumn($this->getDeletedColumnName(), Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
                }
-               if ($this->getHiddenColumnName() !== NULL) {
+               if ($this->hasHiddenColumn()) {
                        $this->addColumn($this->getHiddenColumnName(), Tx_ExtBase_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
                }
        }
@@ -182,17 +188,6 @@ class Tx_ExtBase_Persistence_Mapper_DataMap {
                return $this->columnMaps[$propertyName];
        }
 
-       public function getColumnList() {
-               $columnList = '';
-               foreach ($this->columnMaps as $columnMap) {
-                       if ($columnList !== '') {
-                               $columnList .= ',';
-                       }
-                       $columnList .= $columnMap->getColumnName();
-               }
-               return $columnList;
-       }
-
        /**
         * Returns TRUE if the property is persistable (configured in $TCA)
         *
@@ -204,23 +199,161 @@ class Tx_ExtBase_Persistence_Mapper_DataMap {
        }
 
        /**
+        * Returns TRUE if the table has a pid column holding the id of the page the record is virtually stored on.
+        * Currently we don't support tables without a pid column.
+        *
+        * @return boolean The result
+        */
+       public function hasPidColumn() {
+               // TODO Should we implement a check for having a pid column?
+               return TRUE;
+       }
+
+       /**
+        * Returns the name of a column holding the timestamp the record was modified
+        *
+        * @return string The field name
+        */
+       public function getTimestampColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp'];
+       }
+
+       /**
+        * Returns TRUE if the table has a column holding the timestamp the record was modified
+        *
+        * @return boolean The result
+        */
+       public function hasTimestampColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp']);
+       }
+
+       /**
+        * Returns the name of a column holding the creation date timestamp
+        *
+        * @return string The field name
+        */
+       public function getCreationDateColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate'];
+       }
+
+       /**
+        * Returns TRUE if the table has olumn holding the creation date timestamp
+        *
+        * @return boolean The result
+        */
+       public function hasCreationDateColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate']);
+       }
+
+       /**
+        * Returns the name of a column holding the uid of the back-end user who created this record
+        *
+        * @return string The field name
+        */
+       public function getCreatorUidColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id'];
+       }
+
+       /**
+        * Returns TRUE if the table has a column holding the uid of the back-end user who created this record
+        *
+        * @return boolean The result
+        */
+       public function hasCreatorUidColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id']);
+       }
+
+       /**
         * Returns the name of a column indicating the 'deleted' state of the row
         *
-        * @return string The class name
+        * @return string The field name
         */
        public function getDeletedColumnName() {
                return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete'];
        }
 
        /**
+        * Returns TRUE if the table has a column indicating the 'deleted' state of the row
+        *
+        * @return boolean The result
+        */
+       public function hasDeletedColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete']);
+       }
+
+       /**
         * Returns the name of a column indicating the 'hidden' state of the row
         *
+        * @return string The field name
         */
        public function getHiddenColumnName() {
                return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled'];
        }
 
        /**
+        * Returns TRUE if the table has a column indicating the 'hidden' state of the row
+        *
+        * @return boolean The result
+        */
+       public function hasHiddenColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled']);
+       }
+
+       /**
+        * Returns the name of a column holding the timestamp the record should not displayed before
+        *
+        * @return string The field name
+        */
+       public function getStartTimeColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime'];
+       }
+
+       /**
+        * Returns TRUE if the table has a column holding the timestamp the record should not displayed before
+        *
+        * @return boolean The result
+        */
+       public function hasStartTimeColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime']);
+       }
+
+       /**
+        * Returns the name of a column holding the timestamp the record should not displayed afterwards
+        *
+        * @return string The field name
+        */
+       public function getEndTimeColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime'];
+       }
+
+       /**
+        * Returns TRUE if the table has a column holding the timestamp the record should not displayed afterwards
+        *
+        * @return boolean The result
+        */
+       public function hasEndTimeColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime']);
+       }
+
+       /**
+        * Returns the name of a column holding the uid of the front-end user group which is allowed to edit this record
+        *
+        * @return string The field name
+        */
+       public function getFrontEndUserGroupColumnName() {
+               return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group'];
+       }
+
+       /**
+        * Returns TRUE if the table has a column holding the uid of the front-end user group which is allowed to edit this record
+        *
+        * @return boolean The result
+        */
+       public function hasFrontEndUserGroupColumn() {
+               return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group']);
+       }
+
+       /**
         * Converts a value from a database field type to a property type
         *
         * @param string $className The class name
index c8d586e..1f433b2 100644 (file)
@@ -346,13 +346,13 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
         * @return void
         */
        public function persistAll() {
-               // first, persit all aggregate root objects
+               // first, persist all aggregate root objects
                $aggregateRootClassNames = $this->persistenceSession->getAggregateRootClassNames();
                foreach ($aggregateRootClassNames as $className) {
                        $this->persistObjects($className);
                }
                // persist all remaining objects registered manually
-               // $this->persistObjects();
+               $this->persistObjects();
        }
 
        /**
@@ -365,17 +365,17 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                foreach ($this->persistenceSession->getAddedObjects($className) as $object) {
                        $this->insertObject($object);
                        $this->persistenceSession->unregisterObject($object);
-                       $this->persistenceSession->registerReconstitutedObject($object);
+                       // $this->persistenceSession->registerReconstitutedObject($object);
+               }
+               foreach ($this->persistenceSession->getRemovedObjects($className) as $object) {
+                       $this->deleteObject($object);
+                       $this->persistenceSession->unregisterObject($object);
                }
                foreach ($this->persistenceSession->getDirtyObjects($className) as $object) {
                        $this->updateObject($object);
                        $this->persistenceSession->unregisterObject($object);
                        $this->persistenceSession->registerReconstitutedObject($object);
                }
-               foreach ($this->persistenceSession->getRemovedObjects($className) as $object) {
-                       $this->deleteObject($object);
-                       $this->persistenceSession->unregisterObject($object);
-               }
        }
 
        /**
@@ -401,12 +401,17 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                                $row[$parentTableFieldName] = $parentDataMap->getTableName();
                        }
                }
-
+               
+               if ($dataMap->hasPidColumn()) {
+                       $row['pid'] = !empty($this->cObj->data['pages']) ? $this->cObj->data['pages'] : $GLOBALS['TSFE']->id;
+               }
+               if ($dataMap->hasCreationDateColumn()) {
+                       $row[$dataMap->getCreationDateColumnName()] = time();
+               }
+               if ($dataMap->hasTimestampColumn()) {
+                       $row[$dataMap->getTimestampColumnName()] = time();
+               }
                unset($row['uid']);
-
-               $row['pid'] = !empty($this->cObj->data['pages']) ? $this->cObj->data['pages'] : $GLOBALS['TSFE']->id;
-               $row['tstamp'] = time();
-
                $tableName = $dataMap->getTableName();
                $res = $this->database->exec_INSERTquery(
                        $tableName,
@@ -427,12 +432,7 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                $properties = $object->_getDirtyProperties();
                $dataMap = $this->getDataMap(get_class($object));
                $row = $this->getRow($dataMap, $properties);
-               unset($row['uid']);
-               // TODO Check for crdate column
-               $row['crdate'] = time();
-               if (!empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
-                       $row['cruser_id'] = $GLOBALS['TSFE']->fe_user->user['uid'];
-               }
+
                if ($parentObject instanceof Tx_ExtBase_DomainObject_AbstractDomainObject && $parentPropertyName !== NULL) {
                        $parentDataMap = $this->getDataMap(get_class($parentObject));
                        $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
@@ -446,6 +446,13 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                        }
                }
 
+               unset($row['uid']);
+               if ($dataMap->hasTimestampColumn()) {
+                       $row[$dataMap->getTimestampColumnName()] = time();
+               }
+               if ($dataMap->hasCreatorUidColumn() && !empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
+                       $row[$dataMap->getCreatorUidColumnName()] = $GLOBALS['TSFE']->fe_user->user['uid'];
+               }
                $tableName = $dataMap->getTableName();
                $res = $this->database->exec_UPDATEquery(
                        $tableName,
@@ -469,7 +476,7 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                $relations = $this->getRelations($dataMap, $properties);
 
                $tableName = $dataMap->getTableName();
-               if ($onlySetDeleted === TRUE && !empty($deletedColumnName)) {
+               if ($onlySetDeleted === TRUE && $dataMap->hasDeletedColumn()) {
                        $deletedColumnName = $dataMap->getDeletedColumnName();
                        $res = $this->database->exec_UPDATEquery(
                                $tableName,
@@ -656,7 +663,6 @@ class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Sing
                $dataMap = new Tx_ExtBase_Persistence_Mapper_DataMap($className);
                $dataMap->initialize();
                return $dataMap->isPersistableProperty($propertyName);
-               return $dataMap->isPersistableProperty($propertyName);
        }
 
        /**
index 4e66c20..85c068e 100644 (file)
@@ -143,7 +143,7 @@ class Tx_ExtBase_Persistence_Mapper_DataMap_testcase extends Tx_ExtBase_Base_tes
                $dataMap = new Tx_ExtBase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
                $dataMap->initialize();
                $columnMaps = $dataMap->getColumnMaps();
-               $this->assertEquals(12, count($columnMaps), 'The data map was not initialized (wrong number of column maps set).');
+               $this->assertEquals(10, count($columnMaps), 'The data map was not initialized (wrong number of column maps set).');
        }
        
        public function test_DeletedColumnNameCanBeResolved() {
@@ -166,16 +166,6 @@ class Tx_ExtBase_Persistence_Mapper_DataMap_testcase extends Tx_ExtBase_Base_tes
                $this->assertType('Tx_ExtBase_Persistence_Mapper_ColumnMap', $columnMap, 'The column could not be added.');
        }
        
-       public function test_ColumnListCanBeRetrieved() {
-               $dataMap = new Tx_ExtBase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
-               $dataMap->addColumn('column1');
-               $dataMap->addColumn('column2');
-               $dataMap->addColumn('column3');
-               $dataMap->addColumn('column4');
-               $columnList = $dataMap->getColumnList();
-               $this->assertEquals($columnList, 'column1,column2,column3,column4', 'The column list could not be retrieved.');
-       }
-       
        public function test_PersistablePropertyCanBeChecked() {
                $dataMap = new Tx_ExtBase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
                $dataMap->addColumn('configured_property');
index e537942..afab2c7 100644 (file)
@@ -113,6 +113,8 @@ class Tx_ExtBase_Dispatcher {
                        $controller->processRequest($request, $response);
                } catch (Tx_ExtBase_Exception_StopAction $ignoredException) {
                }
+               // TODO handle argument exceptions
+               // var_dump($persistenceSession);
                $persistenceSession->commit();
                $persistenceSession->clear();