【问题标题】:Seed multiple tables from one backend/builder form?从一个后端/构建器表单中播种多个表?
【发布时间】: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


    【解决方案1】:

    在我看来,您必须对结构进行许多更改,如下所示:

    1) 在 posts 表中添加 content_id

    id, flag1, flag2, content_id, created_at, updated_at
    

    第二个表应该是contents

    id, content
    

    content_id 将用于在 PostContent 之间创建 一对一 关系。更多关于关系的信息click here

    2) 然后,您必须为帖子定义模型Post,为所有内容定义Content

    在内容模型中赋予One to one关系。

    public $hasOne = [
            'post' => 'Test\Gcn\Models\Post'
        ];
    

    如果您给出这种类型的关系,这将在您的 Post 模型中找到 content_id,因此它会在 PostContent 之间设置直接关系>.

    3) 你的表单域应该是这样的

    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)form relation 2)column relation

    这一切都来自我的身边。现在分析所有这些并尝试您的代码。如果您有任何疑问,请告诉我。

    谢谢。

    【讨论】:

    • 您好,首先感谢您的回复。不幸的是,您的代码也不起作用,我完全按照您的说法重新安装并设置了两个表、模型和 .yaml 文件。只是为了确保,在此控制器将为内容创建对吗?
    • 是的,您也必须添加控制器。看来您是 octobercms 的新手。使用此插件开发您的后端Rainlab Builder。您应该简要阅读 octobercms 文档。
    • @guy23234 框架中一般需要三个部分。 1) 模型 2) 视图 3) 控制器。在您新的 OctoberCMS 项目中,您还没有定义控制器。所以这是行不通的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多