[+FEATURE] Extbase (Persistence): Implemented support for multiple valued static...
authorJochen Rau <j.rau@web.de>
Tue, 27 Oct 2009 23:55:06 +0000 (23:55 +0000)
committerJochen Rau <j.rau@web.de>
Tue, 27 Oct 2009 23:55:06 +0000 (23:55 +0000)
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php
typo3/sysext/extbase/Classes/Persistence/Query.php
typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Classes/Persistence/Value.php
typo3/sysext/extbase/Classes/Persistence/ValueFactory.php

index 804b7aa..9af7559 100644 (file)
@@ -282,18 +282,15 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                $parentKeyFieldName = $columnMap->getParentKeyFieldName();
                if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
                        $query = $queryFactory->create($columnMap->getChildClassName());
+                       if (!empty($childSortByFieldName)) {
+                               $query->setOrderings(array($childSortByFieldName => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
+                       }
                        $parentKeyFieldName = $columnMap->getParentKeyFieldName();
                        if (isset($parentKeyFieldName)) {
-                               $query->matching($query->equals($parentKeyFieldName, $parentObject->getUid()));
-                               if (!empty($childSortByFieldName)) {
-                                       $query->setOrderings(array($childSortByFieldName => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
-                               }
-                               $objects = $query->execute();
+                               $objects = $query->matching($query->equals($parentKeyFieldName, $parentObject->getUid()))->execute();
                        } else {
                                $uidArray = t3lib_div::intExplode(',', $fieldValue);
-                               $uids = implode(',', $uidArray);
-                               // FIXME Using statement() is only a preliminary solution
-                               $objects = $query->statement('SELECT * FROM ' . $columnMap->getChildTableName() . ' WHERE uid IN (' . $uids . ')')->execute();
+                               $objects = $query->matching($query->equals('uid', $uidArray))->execute();
                        }
                } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
                        $relationTableName = $columnMap->getRelationTableName();
index ad45e43..f6e9958 100644 (file)
@@ -229,7 +229,15 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
                }
 
                foreach ($this->operands as $name => $value) {
-                       $query->bindValue($name, $this->valueFactory->createValue($value));
+                       if (is_array($value)) {
+                               $newValue = array();
+                               foreach ($value as $valueItem) {
+                                       $newValue[] = $this->valueFactory->createValue($valueItem);
+                               }
+                               $query->bindValue($name, $this->valueFactory->createValue($newValue));
+                       } else {
+                               $query->bindValue($name, $this->valueFactory->createValue($value));
+                       }
                }
                $query->setQuerySettings($this->getQuerySettings());
                return $query;
@@ -410,7 +418,8 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
                                );
                }
 
-               if ($caseSensitive) {
+               // TODO Implement case sensitivity for arrays (callback)
+               if ($caseSensitive || !is_string($operand)) {
                        $this->operands[$uniqueVariableName] = $operand;
                } else {
                        $this->operands[$uniqueVariableName] = strtolower($operand);
index 8e48529..21815dd 100644 (file)
@@ -36,6 +36,8 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
 
        const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
        const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';
+       const OPERATOR_IN = 'operatorIn';
+       const OPERATOR_NOT_IN = 'operatorNotIn';
 
        /**
         * The TYPO3 database object
@@ -184,6 +186,7 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                        $statement = $this->getStatement($query, $parameters);
                }
                $this->replacePlaceholders($statement, $parameters);
+               // debug($statement,-2);
                $result = $this->databaseHandle->sql_query($statement);
                $this->checkSqlErrors();
                return $this->getRowsFromResult($query->getSource(), $result);
@@ -436,9 +439,13 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                                $operator = self::OPERATOR_EQUAL_TO_NULL;
                        } elseif ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO) {
                                $operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
-                       } else {
-                               // TODO Throw exception
                        }
+               } elseif (is_array($value)) {
+                       if ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO) {
+                               $operator = self::OPERATOR_IN;
+                       } elseif ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO) {
+                               $operator = self::OPERATOR_NOT_IN;
+                       }                       
                }
                $parameters[] = $value;
 
@@ -496,6 +503,12 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                        case self::OPERATOR_NOT_EQUAL_TO_NULL:
                                $operator = 'IS NOT';
                                break;
+                       case self::OPERATOR_IN:
+                               $operator = 'IN';
+                               break;
+                       case self::OPERATOR_NOT_IN:
+                               $operator = 'NOT IN';
+                               break;          
                        case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO:
                                $operator = '=';
                                break;
@@ -540,6 +553,12 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                        if ($markPosition !== FALSE) {
                                if ($parameter === NULL) {
                                        $parameter = 'NULL';
+                               } elseif (is_array($parameter)) {
+                                       $items = array();
+                                       foreach ($parameter as $item) {
+                                               $items[] = $this->databaseHandle->fullQuoteStr($item, 'foo');
+                                       }
+                                       $parameter = '(' . implode(',', $items) . ')';
                                } else {
                                        $parameter = $this->databaseHandle->fullQuoteStr($parameter, 'foo'); // FIXME This may not work with DBAL; check this
                                }
index 38ad077..fb83bba 100644 (file)
@@ -66,6 +66,8 @@ class Tx_Extbase_Persistence_Value implements Tx_Extbase_Persistence_ValueInterf
         */
        public function getString() {
                if ($this->value === NULL) return NULL;
+               if (is_array($this->value)) return $this->value;
+
                switch ($this->type) {
                        case Tx_Extbase_Persistence_PropertyType::DATE:
                                if (is_a($this->value, 'DateTime')) {
index 691a80b..a5d2fc5 100644 (file)
@@ -57,7 +57,7 @@ class Tx_Extbase_Persistence_ValueFactory implements Tx_Extbase_Persistence_Valu
         * @throws \IllegalArgumentException if the specified DateTime value cannot be expressed in the ISO 8601-based format defined in the JCR 2.0 specification and the implementation does not support dates incompatible with that format.
         */
        public function createValue($value, $type = Tx_Extbase_Persistence_PropertyType::UNDEFINED, $weak = FALSE) {
-               if (is_array($value)) {
+               if (is_array($value) && array_key_exists('uid', $value)) {
                        $value = $value['uid'];
                }