我正在尝试将数据插入一个(构建器(后端表格/模型中的多个相关表中 - 但是看来我在做错事。
t1帖子(用于模型posts.php(
id, flag1, flag2 , created_at, updated_at
t2 Post_content(用于模型posts_content.php(
id, content
我尝试扩展该模型(posts.php(,用于该表格,如So:
的关系文档中所述public $hasOne = ['content' => ['TestGcnModelsPost_content', 'key' => 'id', 'otherKey' => 'id']];
虽然当通过后端控制器创建记录时,这不会产生错误,但实际上没有将数据写入posts_content。
我还尝试使用代理字段
解决它fields.yaml(模型posts.php(
post_content[content]:
label: 'Post here'
size: ''
mode: tab
span: full
type: markdown
posts.php(带代理字段(
public function formExtendModel($model)
{
/*
* Init proxy field model if we are creating the model
*/
if ($this->action == 'create') {
$model->post_content = new Post_content;
}
return $model;
}
根据错误消息,需要将数组设置为可Json。但是即使在那之后,它似乎正在尝试将数据插入错误的表格。
实现这一目标的正确方法是什么?我试图拥有一种表单,用户可以输入一些标志(复选框等(和一个文本字段,该字段应插入具有正确ID的post_content表中。
感谢您的时间和帮助,谢谢!
从我的看法中,您必须在结构上进行许多更改,如下:
1(在您的 posts
表中添加content_id
id, flag1, flag2, content_id, created_at, updated_at
和第二个表应该是 contents
表
id, content
content_id 将用于创建一对一 POST 和 content 之间的关系。有关关系的更多信息,请单击此处。
2(然后,您必须为帖子定义型号Post
和所有内容的Content
。
在内容模型中给出 One to one
关系。
public $hasOne = [
'post' => 'TestGcnModelsPost'
];
如果给出了这种类型的关系,则在您的帖子模型中找到 content_id
,因此它在 POST 和 content 之间设置了直接关系。
3(您的表单字段应该像
in post 模型fields.yaml
应该是
fields:
flag1:
label: 'Flag 1'
oc.commentPosition: ''
span: left
type: text
flag2:
label: 'Flag 2'
oc.commentPosition: ''
span: auto
type: text
和 content 型号fields.yaml
应该是
fields:
post:
label: Post
oc.commentPosition: ''
nameFrom: flag1
descriptionFrom: description
span: auto
type: relation
content:
label: Content
size: small
oc.commentPosition: ''
span: left
type: textarea
和columns.yaml
应该是
columns:
content:
label: Content
type: text
searchable: true
sortable: true
post:
label: Post
select: flag1
relation: post
searchable: true
sortable: true
有关后端的更多详细信息,您可以去这里... 1(形式关系2(列关系。
这一切都来自我。现在分析所有这些并尝试您的代码。告诉我您是否有查询。
谢谢。