简体   繁体   中英

OctoberCMS: Access parent model in relation form

I am creating an interface in the backend where the parent model has a belongsToMany relationship which is handled via a relation manager widget.

When creating the related model I need to be able to access the model (and relationships) of the model in the parent form in order to populate the child form correctly.

Unfortunately I was not able to find how to do this, can someone shed some light into this problem?

Managed to find out a way to pass the needed data to the form in the relation manager by extending the relation manager widget in the controller and passing the parrent model in the config of the widget making it accessible within the child form.

Here is the code:

public function relationExtendManageWidget($widget, $field, $model)
{
    if($field === 'MyRelationName'
        && property_exists($widget->config, 'context')
    ) {
        $widget->config->parentModel = $model;
       
    }
}

You can also pass a value directly to the child Model like this:

public function relationExtendManageWidget($widget, $field, $model)
{
    if($field === 'MyRelationName'
        && property_exists($widget->config, 'context')
        && $widget->config->context === 'create'
    ) {
        $widget->config->model->customer_id = $model->customer_id;
       
    }
}

If I understand you right. I think you are looking for the relation manager.

This is the section in the documents that you should look at.

"Before relations can be managed on any page, the target model must first be initialized in the controller by calling the initRelation method."

$this->initRelation($post);

Check this awesome video . Note that it was done with v1 of OctoberCMS but besides visually looking off it should still work the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM