【问题标题】:How to set value for additional column in pivot table in OctoberCMS?如何在 OctoberCMS 的数据透视表中设置附加列的值?
【发布时间】:2020-04-08 08:32:30
【问题描述】:

我有doctorspecialization 表,还有doctor_specialization_pivot 表。在我的数据透视表中,我有以下列:

| doctor_id | additional_data | specialization_id |

additional_data 来自doctor 模型以及doctor_id

在我的doctor 模型文件中,我有这样的关系:

public $belongsToMany = [
    'specialization' => [
        'path\to\specialization\model', 
        'table' => 'doctor_specialization_pivot', 
        'parentKey' => 'doctor_id', 
        'otherKey' => 'specialization_id',
    ]
];

现在在提交表单时,我收到了这个错误:

SQLSTATE[HY000]: General error: 1364 Field 'additional_data' doesn't have a default value (SQL: insert into doctor_specialization_pivot (doctor_id, specializations_id) values (1, 3))"

我尝试添加到我的关系'pivot' => ['additional_data']。但仍然得到同样的错误。

我检查了提交的数据,additional_data 不为空。我查看了 OctoberCMS 论坛,但没有得到直接的答案,例如 thisthis

【问题讨论】:

  • 您能说说您是如何保存这些数据的吗?在前端表单还是后端控制器表单中?
  • 另外你能告诉我们你的意思是“additional_data 不为空”吗??

标签: relationship octobercms


【解决方案1】:

好的。我找到了自己问题的答案。

我会详细回答以帮助大家。经过挖掘和盲目射击。根据该文档here,我们可以使用方法attach()通过在连接模型的中间表中插入一条记录来为用户附加角色

文档中让我感到困惑的是它使用了$roleId 变量,而我不明白$roleId 的来源。如果是父表的id或者其他表的id

链接示例:

$user = User::find(1);

$user->roles()->attach($roleId);

所以我在我的doctor 模型中做了什么,我挂钩到事件beforeSave,使用关系($this->specialization)作为第一个参数,而不是文档中的id$this->specialization() 也是在belongsToMany 中定义的关系。

答案:

public function beforeSave()
{
    $this->specialization()->attach($this->specialization,['additional_data' => 'additional data from doctor table']);
}

【讨论】:

  • $roleId 是要附加到父模型的记录(在本例中为角色模型)的 ID 号。所以你可以反过来做$role->users()->attach($userId)。假设。您是否将其保存在后端?或者您是否有使用 php 代码或组件函数的前端?您的问题中从未解释过这一点,两者都会有不同的答案。
  • 我为此使用了 BuilderPlugin,所以我没有更改任何关于保存的内容,它只是 octobercms 的默认设置。我的回答有问题。当我更新我的表单并从表单中更改 specialty_id 的值时。由于attach() 方法,它插入了一条新记录,而不是更新数据透视表记录。所以我正在寻找一个不同的答案,也许有人可以。
  • 因此您将记录保存在后端,例如 example.com/backend/plugin/model/create|update 。是的,我正在为您在工作之间寻找答案。需要看我的笔记。
【解决方案2】:

该实现非常类似于 Watch Learn (Ivan) 的 this video。只需观看他的指南,您就可以了解很多有关 OctoberCMS 的信息。这也是上面的documentation。这是我所做的示例信息。

警告 另一个已知缺陷是您无法将其应用于尚未创建的模型记录。与在附加记录之前等待保存的标准关系小部件不同,它以单独的覆盖形式附加记录。

这是我的model.php 关系:

public $belongsToMany = [
    'equipments' => [
        'Brandon\Pixelrpg\Models\Equipments',
        'table' => 'brandon_pixelrpg_equipment_inventory',
        'key' => 'inventory',
        'otherKey' => 'equipment',
        'pivot' => ['quantity']
    ]
];

这是我的controller.php

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

这是我的config_relation.yaml

equipments:
    label: Equipments
    view:
        list:
            columns:
                id:
                    label: ID
                    type: number
                    searchable: true
                    sortable: true
                name:
                    label: Name
                    type: text
                    searchable: true
                    sortable: true
                value:
                    label: Value
                    type: number
                    searchable: true
                    sortable: true
                updated_at:
                    label: Updated
                    type: datetime
                    searchable: true
                    sortable: true
                pivot[quantity]:
                    label: Quantity
                    type: number

    pivot:
        form:
            fields:
                pivot[quantity]:
                    label: Quantity
                    type: number
                    default: 0

【讨论】:

  • 我看了视频,非常有帮助。但我认为该链接更多地是关于在后端管理/显示关系。我的问题是,当用户提交表单时,我想将表单中的附加值保存到数据库中的关系表中。我得到的答案,解决了添加问题,但必须在更新的问题上继续工作。我们也有类似的问题stackoverflow.com/questions/50241697/… 也没有答案。
  • 我正在检查文档中的事件beforeAttach,但找不到修改属性的方法(或在将其保存到数据库之前添加['additional_data' => 'my data'] ..
  • 我问你是否使用后端提交此信息。您需要显示用户如何提交表单,例如您正在使用的 php 代码。您的问题现在缺少信息。
【解决方案3】:

我只是要提出一个新的答案,并假设这是您需要的,因为您还没有展示任何有关表单工作方式的代码。这就是我从前端表单更新数据透视信息的方式。

model.php中的关系:

public $belongsToMany = [
    'specialization' => [
        'path\to\specialization\model', 
        'table' => 'doctor_specialization_pivot', 
        'parentKey' => 'doctor_id', 
        'otherKey' => 'specialization_id',
        'pivot' => ['additional_data'] //This is required
    ]
];

然后在一些 php 代码中我们称之为onAddSpecialization():

public function onAddSpecialization() {
    //Calling a function to get the doctor id maybe from the signed in user
    $doctor = Doctors::find($this->$doctorId());

    //We get our Specialization from an input
    $specialization = Specialization::find(Input::get('specialization_id'));

    //We get our additional data from an input
    $additional_data = Input::get('additional_data');

    //Now we are going to attach the information
    $doctor->specialization()->attach($specialization, ['additional_data' => $additional_data]);
}

现在是更新附加数据的示例:

public function onUpdateAdditionalData() {
    //Calling a function to get the doctor id maybe from the signed in user
    $doctor = Doctors::find($this->$doctorId());

    //If you get specialization by id from an input. I believe you need to go through the relationship in order to access the correct pivot information.
    $specialization = $doctor->specialization->where('id', Input::get('specialization_id'))->first();

    //Insert the new pivot information
    $specialization->pivot->additional_data = $new_additional_data;

    //Save
    $specialization->pivot->save();
}

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2012-05-04
    • 1970-01-01
    • 2019-10-11
    • 2015-08-13
    相关资源
    最近更新 更多