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

serlo89
serlo89

Hi everybody, i'm trying to extend Post model of Rainlab.Blog plugin because I need to add some pdf attachments to the post. I created a new plugin and i declared the boot function to extend Blog plugin.

public function boot()
{
    Post::extend(function($model)
    {
        // I didn't use $model->attachMany = ['attachments'=>'System\Models\File'] beacuse this overrides default
        // relation and I only want to add another one
        $model->attachMany['attachments'] = 'System\Models\File';
    });
}

It extends correctly the model. If i dump the post i can see all the attachMany relations:

attachMany  array(3)    
array:3 [▼
  "featured_images" => array:2 [▶]
  "content_images" => array:1 [▶]
  "attachments" => "System\Models\File" //Here it is!
]

BUT if I try to extend the backend Form adding a file upload, i have an issue. My boot function is:

public function boot()
{
    Post::extend(function($model) {
        $model->attachMany['attachments'] = 'System\Models\File';
    });

    Posts::extendFormFields(function($form, $model, $context)
    {
        $form->addSecondaryTabFields([
            'attachments' => [
                'label' => 'Attachments',
                'type' => 'fileupload',
                'mode' => 'file',
                'fileTypes' => 'pdf',
                'useCaption' => true,
                'thumbOptions' => [
                    'mode' => 'crop',
                    'extension' => 'auto'
                ],
                'span' => 'full',
                'tab' => 'rainlab.blog::lang.post.tab_manage',
            ]
        ]);
     }
}

And if I go in the backend, trying to add a new post i have the issue:

Model 'System\Models\File' does not contain a definition for the relation 'attachments'.

I looked around for this error and i found that happens when there is no attachMany (or attachOne) relation defined, but i added it before in the same function. Is it a bug or I did something wrong?

Please help me! Thank you all guys.

1-1 of 1

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