我的Octobercms数据透视表获取模型表在我尝试添加时应获得的数据

时间:2016-04-04 02:14:56

标签: laravel pivot-table relationships octobercms

我有一个恼人的问题,使用最新的OctoberCMS版本(318),它试图将错误的数据保存到数据透视表而不是模型表。

我有一个模型企业,以及一个模型Openinghours:

商家模型

`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是包含business_idopeninghours_id

的数据透视表

Openinghours 的模型:

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

在我已完成此操作的业务控制器field.yaml中,为业务添加营业时间:

    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

问题是,10月份试图将开放时间保存到数据透视表而不是模型表。任何人都知道如何解决这个问题?我尝试了很多不同的选择。

这是我在后端得到的错误:

 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_houropen_houropeninghours_idweek_day等位于ekstremedia_emcityportal_openinghours中,在Openinghours模型中定义,而不在{{1}中这是枢轴表...

2 个答案:

答案 0 :(得分:1)

嗨,我认为你的情况是一个复杂的关系场景,并且10月cms作为一个处理这个问题的工具就是relation behavior实际上形式行为对于简单的关系来说就足够了或当你链接时认为AFTER你创造了它们。

在这里,您尝试同时创建一个保存。 有一个pretty good ressource about it,我认为它足以理解关系行为。

但是,开放时间是否属于多个企业?

答案 1 :(得分:0)

您的错误是10月默认表单保护程序的正常行为。

默认情况下,自动生成的表单用于保存十月使用trait Backend\Traits\FormModelSaver。那个只能用于:

  • '属于关联'
  • 'hasOne'
  • 'morphOne'

您可以重新定义保存方法,以允许10月份使用您的模型。例如,我重新定义了创建:     

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;
        }
    }
}