Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Octobercms pivot table gets the data that model table should get when i try to add to it

I have an annoying problem, using latest OctoberCMS build (318), where it tries to save the wrong data to the pivot table instead of model table.

I have a model Businesses, and a model Openinghours:

Model for Businesses:

`public $table = 'ekstremedia_emcityportal_businesses';`
`public $belongsToMany = [
    'openinghours' => [
            'Ekstremedia\EmCityportal\Models\Openinghours',
            'table' => 'ekstremedia_emcityportal_ohb',
            'order' => 'week_day',
            'week_day' => 'week_day',
            'name' => 'week_day',
    ]           
];`    

ekstremedia_emcityportal_ohb is the pivot table with business_id and openinghours_id

And model for Openinghours:

public $table = 'ekstremedia_emcityportal_openinghours';
public $belongsToMany = [
        'businesses' => ['Ekstremedia\EmCityportal\Models\Business',
        'table' => 'ekstremedia_emcityportal_businesses',
        'order' => 'created_at desc'       
  ]
];   

In the Business controllers field.yaml i have done this, to add opening hours to a business:

    openinghours:
        type: repeater
        label: 'Åpningstider'
        tab: 'Åpningstider'
        form:
            fields:
                week_day:
                    label: Dag
                    oc.commentPosition: ''
                    options:
                        1: Måndag
                        2: Tysdag
                        3: Onsdag
                        4: Torsdag
                        5: Fredag
                        6: Laurdag
                        7: Sundag
                    span: left
                    type: dropdown
                open_hour:
                    label: Date added
                    type: datepicker
                    mode: time
                close_hour:
                    mode: time
                    label: Date added
                    type: datepicker

The problem is, october is trying to save the opening hours to the pivot table and not the model table. Anyone have an idea how i can fix that? Ive tried alot of different options. .

This is the error i get on the backend:

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'close_hour' in 'field list' (SQL: insert into 'ekstremedia_emcityportal_ohb' ('business_id', 'close_hour', 'open_hour', 'openinghours_id', 'week_day')...

close_hour, open_hour, openinghours_id, week_day etc, is in ekstremedia_emcityportal_openinghours , defined in Openinghours model, not in ekstremedia_emcityportal_ohb wich is the pivot table...

like image 281
Terje Nesthus Avatar asked Nov 09 '22 17:11

Terje Nesthus


1 Answers

hi i think that your case is a complex relation scenario and for that october cms as a tool to handle this for your it is the relation behavior in fact form behavior is enough for simple relation or when you link thinks AFTER you have created them.

Here you are trying to create an save both at the same time. there is a pretty good ressource about it and i think it is enough to understand relation behavior.

But does openinghours belong to more than one business ?

like image 184
Greg Bee Avatar answered Nov 15 '22 11:11

Greg Bee