Browse Source

Disallowing the use of batching in userland type classes extending Type

This is to avoid breaking backwards compatibility.
Jose Lorenzo Rodriguez 8 years ago
parent
commit
6775312b07
2 changed files with 17 additions and 2 deletions
  1. 17 1
      src/Database/FieldTypeConverter.php
  2. 0 1
      src/Database/Query.php

+ 17 - 1
src/Database/FieldTypeConverter.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Database;
 
+use Cake\Database\Type;
 use Cake\Database\Type\BatchCastingInterface;
 use Cake\Database\Type\OptionalConvertInterface;
 
@@ -40,6 +41,13 @@ class FieldTypeConverter
      */
     protected $batchingTypeMap;
 
+    /**
+     * An array containing all the types registered in the Type system
+     * at the moment this object is created. Used so that the types list
+     * is not fetched on each single row of the results.
+     *
+     * @var array
+     */
     protected $types;
 
     /**
@@ -69,7 +77,15 @@ class FieldTypeConverter
                 continue;
             }
 
-            if ($type instanceof BatchCastingInterface) {
+            // Because of backwards compatibility reasons, we won't allow classes
+            // inheriting Type in userland code to be batchable, even if they implement
+            // the interface. Users can implement the TypeInterface instead to have
+            // access to this feature.
+            $batchingType = $type instanceof BatchCastingInterface &&
+                $type instanceof Type &&
+                strpos(get_class($type), 'Cake\Database\Type') === false;
+
+            if ($batchingType) {
                 $batchingMap[$k] = $type;
                 continue;
             }

+ 0 - 1
src/Database/Query.php

@@ -2094,7 +2094,6 @@ class Query implements ExpressionInterface, IteratorAggregate
         $driver = $this->getConnection()->getDriver();
 
         if ($this->typeCastEnabled && $typeMap->toArray()) {
-            $driver = $this->_connection->getDriver();
             $statement = new CallbackStatement($statement, $driver, new FieldTypeConverter($typeMap, $driver));
         }