Extending Rainlab Pages Model

Hello,

I am trying to extend the RainLab static pages with $BelongsToMany

\RainLab\Pages\Classes\Page::extend(function ($model){
            $model->belongsToMany['blocks'] = [
                CmsBlock::class,
                'table' => 'my_cmsblocks_list_to_cms',
                'key' => 'entity_id',
                'pivotSortable' => 'sort_order',
            ];
        });

        \RainLab\Pages\Controllers\Index::extend(function($controller){
            $controller->implementClassWith(\Backend\Behaviors\RelationController::class);
            $controller->addDynamicProperty('relationConfig', '$/my/cmsblocks/controllers/blocksincmscontroller/config_relation.yaml');
        });

I am getting an error

Relation behavior used in RainLab\Pages\Controllers\Index does not have a model defined.

Any idea What i am doing wrong here?

Thank you in advance

few things appears to me here:
you need to check that you are dealing with the proper model, that it is already or not extended with the relationController behavior and if so you need to merge the relationConfig instead of adding a new one.
so it looks like this.
note: untested code

\RainLab\Pages\Controllers\Index::extend(function($controller){
            if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
                $controller->implementClassWith(\Backend\Behaviors\RelationController::class);
            }
            if (!isset($controller->relationConfig)) {
                $controller->addDynamicProperty('relationConfig');
            }
            $controller->mergeConfig('relationConfig', '$/my/cmsblocks/controllers/blocksincmscontroller/config_relation.yaml');
        });

1 Like

Thank you @chris
unfortunately it does not work.

Unable to find configuration file relationConfig defined for RainLab\Pages\Controllers\Index.

the config_relation.yaml path is correct.

The merge config will try to find the config A which in this case is not present. Even if you trick october cms and provide a Dummy config.yaml and merge both of them. It results in the same error as Model not found.

I have it to work in many of my projects.

the code should be

$controller->relationConfig = $controller->mergeConfig(
    $controller->relationConfig,
    '$/my/cmsblocks/controllers/blocksincmscontroller/config_relation.yaml'
);

Thank you @chris .

For some reasons it is still the same as model not found. It works with other plugins bit not with rainlab pages.