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

eckelcn
eckelcn

I extends the RelationController class, but cause an error:

Relation behavior used in Custom\Site\Controllers\Activities does not have a model defined. 
 Location: modules\backend\behaviors\RelationController.php line 296

My source is simple:

class Activities extends Controller
{
    public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController','Custom\Site\Behaviors\RelationController'];

    ...
}

class RelationController extends \Backend\Behaviors\RelationController
{   
     public function __construct($controller)
     {
         parent::__construct($controller);
     }

    protected function makeViewWidget()
     {
        // TODO
        return parent::makeViewWidget();
    }
}

Anybody has an idea what could go wrong?

JeffGoldblum
JeffGoldblum

It doesn't work because the FormController behavior never initializes your custom behavior. Check out the following code in the FormController behavior:

if ($this->controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
    $this->controller->initRelation($model);
}

Your controller isn't being extended with Backend.Behaviors.RelationController, it's being extended with Custom.Site.Behaviors.RelationController which means that your custom behavior never gets properly initialized by the FormController.

JeffGoldblum
JeffGoldblum

You could potentially try to initialize your behavior yourself via the use of formExtendModel() in your base controller. Something along the lines of

class Activities extends Controller
{
    public $implement = [
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',
        'Custom\Site\Behaviors\RelationController'
    ];

    public function formExtendModel($model)
    {
        $this->initRelation($model);
    }
}
eckelcn
eckelcn

LukeTowers said:

You could potentially try to initialize your behavior yourself via the use of formExtendModel() in your base controller. Something along the lines of

class Activities extends Controller
{
   public $implement = [
       'Backend\Behaviors\ListController',
       'Backend\Behaviors\FormController',
       'Custom\Site\Behaviors\RelationController'
   ];

   public function formExtendModel($model)
   {
       $this->initRelation($model);
   }
}

thanks! It works, and need copy all *.htm to my extention controler's folder.

alpapan
alpapan

Thank you for that! Apologies to resurrect a zombie thread but:

This is necessary when: a) you have create a custom view with formwidget in your controller

For example in a Controller:

    public function someview(){
      $model = new \author\plugin\Models\Controller;
      $config = $this->makeConfig('$/alpapan/biobench/models/seqtop/fields_upload_fasta.yaml');
      $config->model = $model ;
      $widget = $this->makeWidget('Backend\Widgets\Form', $config);
      $widget->bindToController();
      $this->vars['widget'] = $widget;
}

It was tricky to realise what is happening and this post didn't come high on Google.

The solution is to add the init at the end:

      $this->initRelation($model);

I don't know where in the documentation this should go but I hope it can go somewhere

1-5 of 5

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