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

haralake
haralake

I try to create a belongsTo relation using a pivot table.

What i have is two models entry and location A entry belongsTo location A location hasMany entries

On Entry model i have:

public $belongsTo = [
    'locations' => [
        'Author\Plugin\Models\Location'
    ]
];

On Location model i have:

public $hasMany = [
    'entries' => ['Author\Plugin\Models\Entry'
    ]
];

On Entry model fields.yaml i have:

    locations:
        label: Locations
        nameFrom: name
        descriptionFrom: description
        span: left
        type: relation
        placeholder: '-- select a status --'
        tab: Locations

On backend when try to create entry i get all locations on dropdown without problem, but when i select one and try to save entry i get error:

Column not found 1054 Unknown column 'location_id' on fields list. This is because it try to save a column from pivot table to entry table. How can i save it to pivot table? I try to use:

public $belongsTo = [
    'locations' => [
        'Author\Plugin\Models\Location',
        'table' => 'author_plugin_entries_loc', /*(this is pivot table)*/
    ]
];

But without success. If i use belongsToMany everything is working fine but i need belongsTo relation.

I don't know if i can use this kind of relation with pivot table, that i need.

Last updated

mjauvin
mjauvin

@haralake the pivot table is only used for MANY-TO-MANY relations, so you'd need to use $belongsToMany[] in both models.

(see http://octobercms.com/docs/backend/relations#belongs-to-many-pivot)

1-2 of 2

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