【发布时间】:2018-03-25 13:26:34
【问题描述】:
我正在尝试将数据从一个(构建器)后端表单/模型插入到多个相关表中 - 但似乎我做错了。
t1 帖子(用于模型 Posts.php)
id, flag1, flag2 , created_at, updated_at
t2 post_content(用于模型 Posts_content.php)
id, content
我已经尝试扩展用于表单的模型 (Posts.php),如 octobercms 的关系文档中所述,如下所示:
public $hasOne = ['content' => ['Test\Gcn\Models\Post_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;
}
根据报错信息,需要将数组设置为jsonable。但即使在那之后,它看起来也试图将数据插入错误的表中。
实现这一目标的正确方法是什么? 我正在尝试使用一种表单,用户可以在其中输入一些标志(复选框等)和一个文本字段,该字段应该以正确的 id 插入到 post_content 表中。
感谢您的时间和帮助,谢谢!
【问题讨论】:
标签: php laravel octobercms