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

I have a nasty problem using the latest assembly of OctoberCMS (318), where it tries to save invalid data in a pivot table instead of a model table.

I have a Businesses model and model watches:

Model for enterprises:

`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 - pivot table with business_id and openinghours_id

And the model to open:

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

In the field.yaml business controllers field.yaml I did this to add hours of work to the 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 that October tries to save hours of work in the pivot table, not in the model table. Anyone have an idea how I can fix this? Ive tried many different options ..

This is the error I get on the server:

  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 the Openinghours model, and not in ekstremedia_emcityportal_ohb , which is a pivot table ...

+5
source share
2 answers

Hi, I think your case is a complex relationship scenario, and for this October cms as a tool to handle this for you, this relationship behavior is actually a form behavior for a simple relationship or when you connect thoughts after you connect them created.

Here you are trying to create a save at the same time. there is a pretty good ressource about this , and I think this is enough to understand relationship behavior.

But does opening a watch really apply to more than one business?

+1
source

Your mistake is the normal behavior of the default form splash screen in October.

By default, for automatic generated form to save October, use trait Backend\Traits\FormModelSaver . This can only work with:

  • 'belongsTo'
  • 'hasOne'
  • 'morphOne'

You can override the save method so that October works with your model. For example, I override create:

 namespace Local\Test\Controllers; class Business extends Controller { use \Backend\Traits\FormModelSaver; public $implement = ['Backend\Behaviors\ListController','Local\Test\Controllers\FormController']; } namespace Local\Test\Controllers; use Backend\Behaviors\FormController as BaseFormController; use Local\Test\Models\Openinghours; use Flash; class FormController extends BaseFormController { /** * Ajax handler for saving from the creation form. * @return mixed */ public function create_onSave($context = null) { $this->context = strlen($context) ? $context : $this->getConfig('create[context]', self::CONTEXT_CREATE); $model = $this->controller->formCreateModelObject(); $this->initForm($model); $this->controller->formBeforeSave($model); $this->controller->formBeforeCreate($model); $savedData = $this->formWidget->getSaveData(); $openHours = $savedData['openinghours']; unset($savedData['openinghours']); $modelsToSave = $this->prepareModelsToSave($model, $savedData); foreach ($modelsToSave as $modelToSave) { $modelToSave->save(null, $this->formWidget->getSessionKey()); } foreach ($openHours as $formOpeninghours ) { $oph = new Openinghours(); $oph->week_day = $formOpeninghours['week_day']; $oph->open_hour = $formOpeninghours['open_hour']; $oph->close_hour = $formOpeninghours['close_hour']; $model->openinghours()->save($oph); } $this->controller->formAfterSave($model); $this->controller->formAfterCreate($model); Flash::success($this->getLang('create[flashSave]', 'backend::lang.form.create_success')); if ($redirect = $this->makeRedirect('create', $model)) { return $redirect; } } } 
0
source

Source: https://habr.com/ru/post/1246369/


All Articles