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;
}
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;
}
}
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());
}
}
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;
}