]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 5 Nov 2021 10:56:32 +0000 (11:56 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 5 Nov 2021 10:56:32 +0000 (11:56 +0100)
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/SelectFromArray.php
src/app/Magic/Fields/SelectFromModel.php

index cfc37ad87a9ed52e962255a9ae27463d932b946d..1447d74c24621d5a7772210fc79b86bc29048608 100644 (file)
@@ -42,6 +42,7 @@ class Field implements \ArrayAccess
     protected $_databaseUnique = false;
     protected $_databaseIndex = false;
     protected $_databaseDefault = null;
+    protected $_databaseLength = null;
 
     /** @var bool|string|array */
     protected $_cast = false;
@@ -145,7 +146,7 @@ class Field implements \ArrayAccess
             'default' => null, 'cast' => $this->_cast, 'column_view_namespace' => $this->_columnViewNamespace, 'searchLogic' => $this->_searchLogic,
             'allow_null' => true,
             'can' => $this->_can, 'can_write' => $this->_canWrite, 'auth' => $this->_auth,
-            'database_type' => $this->_databaseType, 'database_unique' => $this->_databaseUnique, 'database_index' => $this->_databaseIndex, 'database_default' => $this->_databaseDefault,
+            'database_type' => $this->_databaseType, 'database_unique' => $this->_databaseUnique, 'database_index' => $this->_databaseIndex, 'database_default' => $this->_databaseDefault, 'database_length' => $this->_databaseLength,
             'fake' => false, 'store_in' => 'extras', 'attributes' => []];
     }
 
@@ -307,6 +308,10 @@ class Field implements \ArrayAccess
         if ($default !== null) {
             $attributes['default'] = $default;
         }
+        $length = $this->getDatabaseLength();
+        if (null !== $length) {
+            $attributes['length'] = $length;
+        }
 
         $table->addColumn($name,
             CubistMagicAbstractModel::toDoctrineType($this->getDatabaseType()),
@@ -321,6 +326,10 @@ class Field implements \ArrayAccess
         }
     }
 
+    public function getDatabaseLength(){
+        return $this->getAttribute('database_length');
+    }
+
     /**
      * @return null|string
      */
index d571dacd451666214c73a9481c59546368484797..c6aab5d0b789806245c63150fcf6e84c19226980 100644 (file)
@@ -13,6 +13,7 @@ class SelectFromArray extends Field
     protected $_columnType = 'select_from_array';
     protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
     protected $_databaseType = 'string';
+    protected $_databaseLength = 32;
     protected $_multiple = false;
     protected $_allowNull = true;
     protected $_options = [];
@@ -32,6 +33,17 @@ class SelectFromArray extends Field
         }
     }
 
+    public function getDatabaseLength()
+    {
+        $keys = array_keys($this->getAttribute('options'));
+        $max = 0;
+        foreach ($keys as $key) {
+            $max = max($max, mb_strlen($key));
+        }
+        return $max * 2;
+    }
+
+
     public function getOptions()
     {
         return $this->_options;
index c38bf97c396ca5bfb1025fe44031ba4f96c6240a..c2705ec9e68629579648b6c232ac1e54819fcb7b 100644 (file)
@@ -15,6 +15,7 @@ class SelectFromModel extends Model
     protected $_columnType = 'select_from_array';
 
     protected $_databaseType = 'string';
+    protected $_databaseLength=16;
     protected $_multiple = false;
     protected $_order = false;
     protected $_allows_null = false;