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

TJMB
TJMB

Hello all,

I'm developing a site that has a data for Projects and Publications. Each Project can have multiple related Publications, so I'm setting up a polymorphic relationship between the two. I'd like to use a Relation Manager Widget in my form yet I'm encountering an issue. I'm using the Builder plugin to get a starting point and have made some modifications to the models it created.

Here's what I have set up so far:

A polymorphic data table named rdd_nic_pubrelations, with the following fields:

publication_id integer
pubrelation_id integer
pubrelation_type string

In my Publications model, I have:

public $morphedByMany = [
    'projects' => [
        'Rdd\Nic\Models\Projects',
        'name' => 'pubrelation'
    ]
];

In my Projects model, I have:

public $morphToMany = [
    'publications' => [
        'Rdd\Nic\Models\Publications',
        'table' => 'rdd_nic_pubrelations',
        'foreignKey' => 'id',
        'otherKey' => 'pubrelation_id',
        'name' => 'pubrelation'
    ],
];

In my models/projects/fields.yaml file I have:

publications:
    label: 'Related Publications'
    nameFrom: title
    descriptionFrom: authors
    span: right
    type: partial
    path: field_publications

In my Projects controller I have:

public $implement = [
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',
        'Backend\Behaviors\ReorderController',
        'Backend\Behaviors\RelationController'
];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public $relationConfig = 'config_relation.yaml';

In my controllers/projects/config_relation.yaml file I have:

publications:
    label: Related Publications
    view:
        list: $/rdd/nic/models/publications/columns.yaml
        toolbarButtons: add|remove
    manage:
        showSearch: true
        form: $/rdd/nic/models/publications/fields.yaml
        list: $/rdd/nic/models/publications/columns.yaml
        recordsPerPage: 10

I also have a partial controllers/projects/_field_publications.htm that looks like this:

<?= $this->relationRender('publications') ?>

Everything works fine in the backend form, up until I have selected the publications and click the "Add selected" button in the relation manager modal popup. Then I get the following error:

"SQLSTATE[HY000]: General error: 1364 Field 'publication_id' doesn't have a default value (SQL: insert into `rdd_nic_pubrelations` (`pubrelation_id`, `pubrelation_type`) values (1, rdd\Nic\Models\Projects))" on line 664 of /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php

From what I can tell, it appears that the Relation Manager interface isn't passing a value for the publication_id field in the rdd_nic_pubrelations table. I've been through countless searches in Google and in the October and all that I've found is old information that doesn't seem to apply to the current version of October (I'm running October Build 437).

Polymorphic relationships seem to be supported in the current version of Backend\Behaviors\RelationController though it seems there's something that I'm missing in order to get this working properly.

Hopefully someone here can help. Thanks in advance!

Last updated

1-1 of 1

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