From 855e2e42b3751b9004cb88dad6cf5cf7808ee1e8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 12 Jun 2019 14:35:30 +0200 Subject: [PATCH] #2810 --- src/app/Console/Commands/MigrateCommand.php | 2 +- src/app/Magic/Fields/Model.php | 18 ++++++++++++------ src/app/Magic/Fields/SelectFromModel.php | 9 +++++---- .../Magic/Fields/SelectFromModelMultiple.php | 16 +--------------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/app/Console/Commands/MigrateCommand.php b/src/app/Console/Commands/MigrateCommand.php index 735cab9..0fc1f45 100644 --- a/src/app/Console/Commands/MigrateCommand.php +++ b/src/app/Console/Commands/MigrateCommand.php @@ -70,7 +70,7 @@ class MigrateCommand extends CubistCommand $this->line('Handling ' . get_class($model)); $res=$model->setSchema($this->_schema); if(null!==$res) { - $this->line($res); + $this->line(print_r($res,true)); } } } diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 9027522..11d8774 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -3,13 +3,14 @@ namespace Cubist\Backpack\app\Magic\Fields; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; + class Model extends Field { public function getColumnData() { $res = parent::getColumnData(); $res['model'] = $this->getAttribute('model'); - $res['entity'] = $this->getAttribute('entity'); $res['attribute'] = $this->getAttribute('attribute'); return $res; } @@ -19,12 +20,17 @@ class Model extends Field return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'allow_null' => false, 'allow_multiples' => $this->_multiple]); } - protected function _postSetAttributes() + protected function _getOptions() { - $this->setAttributeIfNotSet('entity', $this->_getEntityPrefix() . $this->getAttribute('name')); - } + $modelClass = $this->getAttribute('model'); + /** @var CubistMagicAbstractModel[] $items */ + $items = $modelClass::all(); + $options = []; - protected function _getEntityPrefix(){ - return 'entity_'; + $name = $this->getAttribute('attribute'); + foreach ($items as $item) { + $options[$item->getPrimaryKey()] = $item->{$name}; + } + return $options; } } diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index ea3c9ea..4db6a3d 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -6,13 +6,14 @@ namespace Cubist\Backpack\app\Magic\Fields; class SelectFromModel extends Model { - protected $_adminType = 'select2'; + protected $_adminType = 'select2_from_array'; + protected $_columnType = 'select_from_array'; protected $_databaseType = 'text'; - protected $_columnType = 'select'; protected $_multiple = false; - public function getRelationship() + public function _postSetAttributes() { - return 'belongsTo'; + parent::_postSetAttributes(); + $this->setAttribute('options', $this->_getOptions()); } } diff --git a/src/app/Magic/Fields/SelectFromModelMultiple.php b/src/app/Magic/Fields/SelectFromModelMultiple.php index 0cffde1..7187d09 100644 --- a/src/app/Magic/Fields/SelectFromModelMultiple.php +++ b/src/app/Magic/Fields/SelectFromModelMultiple.php @@ -6,19 +6,5 @@ namespace Cubist\Backpack\app\Magic\Fields; class SelectFromModelMultiple extends SelectFromModel { - protected $_adminType = 'select2_multiple'; - - public function getRelationship() - { - return 'belongsToMany'; - } - - public function getDefaultAttributes() - { - return array_merge(parent::getDefaultAttributes(), ['fillable' => false, 'pivot' => true]); - } - - protected function _getEntityPrefix(){ - return ''; - } + protected $_multiple = true; } -- 2.39.5