[英]OctoberCMS Rainlab.Builder- Error on “hasOne” relation to same model
I'm developing an October CMS Plugin for managing animals (with rainlab.builder). 我正在开发用于管理动物的October CMS插件(使用rainlab.builder)。 Animals have a couple of fields and relations. 动物具有两个领域和关系。 Every animal have a father and a mother animal. 每个动物都有父亲和母亲。 But when I try to safe my animal the following error appears: 但是,当我尝试保护动物安全时,会出现以下错误:
Event Log: 事件日志:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/
Illuminate/Database/Connection.php:413
Plugin animal form: 插件动物形式:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id'
cannot be null (SQL: update `prefix_animals_animal` set `id` = where
`prefix_animals_animal`.`id` = 1 and `prefix_animals_animal`.`id` is
not null)" on line 666 of dir/website/vendor/laravel/framework/
src/Illuminate/Database/Connection.php
The error only appers when I use the relations (child -> father, child -> mother). 该错误仅在使用关系时适用(孩子->父亲,孩子->母亲)。
I have implemented the following hasOne - relations to my animal model : 我实现了以下hasOne-与我的动物模型的关系:
/* Relation */
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
]
];
This are my fields from field.yaml : 这是我来自field.yaml的字段:
father:
label: Father
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No father'
span: left
type: relation
mother:
label: Mother
span: right
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No mother'
type: relation
I would be very happy if someone have a solution for these kind of relations. 如果有人对这种关系有解决方案,我将非常高兴。 Cheerio! 加油!
Don't use id
as the primary key for the relation as this is not good practice. 不要将id
用作关系的主键,因为这不是好习惯。
Actually this creates the problem you are seeing also. 实际上,这也会引起您所看到的问题。 Currently your model is using the id
for primary Id, mother Id and father Id. 当前,您的模型使用的id
为主要ID,母亲ID和父亲ID。 You can't do that. 你不能那样做。
Add mother_id
and father_id
to the Animal model and change the relation definition to this: 将mother_id
和father_id
添加到Animal模型中,并将关系定义更改为此:
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'father_id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'mother_id',
'otherKey' => 'id'
]
];
PS. PS。 In your case you dont have to define key
and otherKey
as the default value for otherKey
is "id" and the otherKey
value is created from the relation name with the "_id" suffix. 你的情况,你没有定义key
和otherKey
作为默认值otherKey
是“ID”和otherKey
从用“_id”后缀关系名所创造的价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.