From aebc83e4cdfe6d38a77eab53dc5c9b8fa931e775 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 5 Nov 2021 11:56:32 +0100 Subject: [PATCH] wip #3753 @0.25 --- src/app/Magic/Fields/Field.php | 11 ++++++++++- src/app/Magic/Fields/SelectFromArray.php | 12 ++++++++++++ src/app/Magic/Fields/SelectFromModel.php | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index cfc37ad..1447d74 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -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 */ diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index d571dac..c6aab5d 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -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; diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index c38bf97..c2705ec 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -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; -- 2.39.5