[+BUGFIX] Extbase (Persistence): Extbase still used PHPs current() on some QueryResul...
authorBastian Waidelich <bastian@typo3.org>
Sun, 14 Nov 2010 14:56:14 +0000 (14:56 +0000)
committerBastian Waidelich <bastian@typo3.org>
Sun, 14 Nov 2010 14:56:14 +0000 (14:56 +0000)
[+TASK] Extbase (Persistence): added a private field to the QueryResult to make the above case easier to debug: When calling current() on an Iterator, PHP returns the first field of that object instead of calling the current() method of the Iterator interface.. With our somewhat pragmatic approach you'll see the warning if you debug the results of current($query->execute())
[+BUGFIX] Extbase (Persistence): Replaced two occurrences of Query->count() by Query->execute()->count() to avoid deprecated warnings in the Core

typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php
typo3/sysext/extbase/Classes/Persistence/QueryResult.php
typo3/sysext/extbase/Classes/Persistence/Repository.php

index c9ec6d6..0e5649c 100644 (file)
@@ -485,7 +485,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
         */
        public function countRelated(Tx_Extbase_DomainObject_DomainObjectInterface $parentObject, $propertyName, $fieldValue = '') {
                $query = $this->getPreparedQuery($parentObject, $propertyName, $fieldValue);
-               return $query->count();
+               return $query->execute()->count();
        }
 
        /**
index 06c4489..9e3f15b 100644 (file)
 class Tx_Extbase_Persistence_QueryResult implements Tx_Extbase_Persistence_QueryResultInterface {
 
        /**
+        * This field is only needed to make debugging easier:
+        * If you call current() on a class that implements Iterator, PHP will return the first field of the object
+        * instead of calling the current() method of the interface.
+        * We use this unusual behavior of PHP to return the warning below in this case.
+        *
+        * @var string
+        * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
+        */
+       private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the Tx_Extbase_Persistence_QueryResult. To retrieve the first result, you can use the getFirst() method.';
+
+       /**
         * @var Tx_Extbase_Persistence_Mapper_DataMapper
         */
        protected $dataMapper;
index b7b66dd..f26752b 100644 (file)
@@ -247,7 +247,7 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
         * @api
         */
        public function countAll() {
-               return $this->createQuery()->count();
+               return $this->createQuery()->execute()->count();
        }
 
        /**
@@ -278,10 +278,10 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
                        $query = $this->createQuery();
                        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
                        $query->getQuerySettings()->setRespectStoragePage(FALSE);
-                       $result = $query->matching($query->equals('uid', $uid))->execute();
-                       $object = NULL;
-                       if (count($result) > 0) {
-                               $object = current($result);
+                       $object = $query->matching($query->equals('uid', $uid))
+                                       ->execute()
+                                       ->getFirst();
+                       if ($object !== FALSE) {
                                $this->identityMap->registerObject($object, $uid);
                        }
                }
@@ -317,18 +317,16 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi
                } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
                        $propertyName = strtolower(substr(substr($methodName, 9), 0, 1) ) . substr(substr($methodName, 9), 1);
                        $query = $this->createQuery();
-                       $result = $query->matching($query->equals($propertyName, $arguments[0]))
+                       $object = $query->matching($query->equals($propertyName, $arguments[0]))
                                ->setLimit(1)
-                               ->execute();
-                       $object = NULL;
-                       if (count($result) > 0) {
-                               $object = current($result);
-                       }
+                               ->execute()
+                               ->getFirst();
                        return $object;
                } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
                        $propertyName = strtolower(substr(substr($methodName, 7), 0, 1) ) . substr(substr($methodName, 7), 1);
                        $query = $this->createQuery();
                        $result = $query->matching($query->equals($propertyName, $arguments[0]))
+                               ->execute()
                                ->count();
                        return $result;
                }