relationName = $this->formField->columnName; $this->relationType = $this->model->getRelationType($this->relationName); $this->nameColumn = $this->getConfig('nameColumn', $this->nameColumn); $this->descriptionColumn = $this->getConfig('descriptionColumn', $this->descriptionColumn); $this->emptyOption = $this->getConfig('emptyOption'); if (!$this->model->hasRelation($this->relationName)) throw new SystemException(Lang::get('backend::lang.model.missing_relation', ['class'=>get_class($this->controller), 'relation'=>$this->relationName])); } /** * {@inheritDoc} */ public function render() { $this->prepareVars(); return $this->makePartial('relation'); } /** * Prepares the view data */ public function prepareVars() { $this->vars['field'] = $this->makeRenderFormField(); } /** * Makes the form object used for rendering a simple field type */ protected function makeRenderFormField() { $field = clone $this->formField; $relatedObj = $this->model->makeRelation($this->relationName); $query = $relatedObj->newQuery(); if (in_array($this->relationType, ['belongsToMany', 'morphToMany', 'morphedByMany'])) { $field->type = 'checkboxlist'; } else if ($this->relationType == 'belongsTo') { $field->type = 'dropdown'; $field->placeholder = $this->emptyOption; } // It is safe to assume that if the model and related model are of // the exact same class, then it cannot be related to itself if ($this->model->exists && (get_class($this->model) == get_class($relatedObj))) { $query->where($relatedObj->getKeyName(), '<>', $this->model->id); } if (in_array('October\Rain\Database\Traits\NestedTree', class_uses($relatedObj))) $field->options = $query->listsNested($this->nameColumn, $relatedObj->getKeyName()); else $field->options = $query->lists($this->nameColumn, $relatedObj->getKeyName()); return $this->renderFormField = $field; } /** * {@inheritDoc} */ public function getSaveData($value) { if (is_string($value) && !strlen($value)) return null; if (is_array($value) && !count($value)) return null; return $value; } }