This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Scott
Scott

After looking around in the RelationController.php file, I found they appear not support morphToMany / morphByMany. How huge of a headache would it be to get these working for polymorphic relationships?

ukebako8008
ukebako8008

You can just extend the RelationController and FromController and update a couple of methods to get it working.

class RelationController extends \Backend\Behaviors\RelationController
{

    /**
     * Create a new related model
     */
    public function onRelationManageCreate()
    {
        $this->forceManageMode = 'form';
        $this->beforeAjax();
        $saveData = $this->manageWidget->getSaveData();

        if ($this->viewMode == 'multi') {
            $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey(true) : null;

            if ($this->relationType == 'hasMany') {
                $newModel = $this->relationObject->create($saveData, $sessionKey);
            }
            elseif ($this->relationType == 'belongsToMany') {
                $newModel = $this->relationObject->create($saveData, [], $sessionKey);
            }
            elseif ($this->relationType == 'morphMany') {
                $newModel = $this->relationObject->create($saveData);
            }

            $newModel->commitDeferred($this->manageWidget->getSessionKey());
        }
        elseif ($this->viewMode == 'single') {
            $newModel = $this->viewModel;
            $this->viewWidget->setFormValues($saveData);

            if ($this->relationType == 'belongsTo') {
                $newModel->save();
                $this->relationObject->associate($newModel);
                $this->relationObject->getParent()->save();
            }
            elseif ($this->relationType == 'hasOne') {
                $this->relationObject->add($newModel);
            }
        }

        return $this->relationRefresh();
    }

    /**
     * Determine the view mode based on the model relationship type.
     * @return string
     */
    protected function evalViewMode()
    {
        if ($this->forceViewMode) {
            return $this->forceViewMode;
        }

        switch ($this->relationType) {
            case 'hasMany':
            case 'belongsToMany':
            case 'morphMany':
                return 'multi';

            case 'hasOne':
            case 'belongsTo':
                return 'single';
        }
    }

}
ukebako8008
ukebako8008

... and the FormController

class FormController extends \Backend\Behaviors\FormController
{

    public function initForm($model, $context = null)
    {
        if ($context !== null)
            $this->context = $context;

        $context = $this->formGetContext();

        /*
         * Each page can supply a unique form definition, if desired
         */
        $formFields = $this->config->form;

        if ($context == self::CONTEXT_CREATE) {
            $formFields = $this->getConfig('create[form]', $formFields);
        }
        elseif ($context == self::CONTEXT_UPDATE) {
            $formFields = $this->getConfig('update[form]', $formFields);
        }
        elseif ($context == self::CONTEXT_PREVIEW) {
            $formFields = $this->getConfig('preview[form]', $formFields);
        }

        $config = $this->makeConfig($formFields);
        $config->model = $model;
        $config->arrayName = class_basename($model);
        $config->context = $context;

        /*
         * Form Widget with extensibility
         */
        $this->formWidget = $this->makeWidget('Backend\Widgets\Form', $config);

        $this->formWidget->bindEvent('form.extendFieldsBefore', function () {
            $this->controller->formExtendFieldsBefore($this->formWidget);
        });

        $this->formWidget->bindEvent('form.extendFields', function ($fields) {
            $this->controller->formExtendFields($this->formWidget, $fields);
        });

        $this->formWidget->bindEvent('form.beforeRefresh', function ($saveData) {
            return $this->controller->formExtendRefreshData($this->formWidget, $saveData);
        });

        $this->formWidget->bindEvent('form.refreshFields', function ($fields) {
            return $this->controller->formExtendRefreshFields($this->formWidget, $fields);
        });

        $this->formWidget->bindEvent('form.refresh', function ($result) {
            return $this->controller->formExtendRefreshResults($this->formWidget, $result);
        });

        $this->formWidget->bindToController();

        /*
         * Detected Relation controller behavior
         */
        if ($this->controller->isClassExtendedWith('Dotted.Name.Space.Of.Your.Extension.RelationController')) {
            $this->controller->initRelation($model);
        }

        $this->prepareVars($model);
        $this->model = $model;
    }

}

Last updated

shoguniphicus
shoguniphicus

This is nice, thanks. Was having problem with the relationcontroller. Thanks alot!

slowpokefarm
slowpokefarm

Also this is required in the RelationController extension:

    /**
     * Determine the management mode based on the relation type and settings.
     * @return string
     */
    protected function evalManageMode()
    {
        if ($mode = post(self::PARAM_MODE)) {
            return $mode;
        }

        if ($this->forceManageMode) {
            return $this->forceManageMode;
        }

        switch ($this->eventTarget) {
            case 'button-create':
            case 'button-update':
                return 'form';

            case 'button-link':
                return 'list';
        }

        switch ($this->relationType) {
            case 'belongsTo':
                return 'list';

            case 'belongsToMany':
                if (isset($this->config->pivot)) return 'pivot';
                elseif ($this->eventTarget == 'list') return 'form';
                else return 'list';

            case 'hasOne':
            case 'hasMany':
            case 'morphMany':
                if ($this->eventTarget == 'button-add') return 'list';
                else return 'form';
        }
    }

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.