十月CMS与自我的关系 [英] October CMS relation to self

查看:180
本文介绍了十月CMS与自我的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有事件模型,并希望能够选择其中的其他事件以显示在前端.

I have the Event model and want to be able to select other events in it to display on the frontend.

我首先尝试的是关系,但是问题是我不知道如何使用数据透视表,因为实际上,它应该包含两个字段,并且两个字段都应命名为"event_id",这显然是不可能的. 也许有人可以建议任何方法来避免这种情况,或者通过事件模型中的事件选择字段来解决我的问题的更好方法?

The first thing that I tried is the relations, but the problem is I don't know how to use the pivot table because actually, it should contain two fields and both should be named as 'event_id' which is obviously impossible. Maybe someone can suggest any way to avoid this or a better way to solve my problem with events choice field in Event model?

谢谢.

推荐答案

是的same name for relation_id是不可能的,但是我们可以选择:)

yes same name for relation_id could be impossible, BUT we have option :)

您可以像这样创建中间表

you can create intermediate table like this

<?php namespace HardikSatasiya\SoTest\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class BuilderTableCreateHardiksatasiyaSotestEventsEvents extends Migration
{
    public function up()
    {
        Schema::create('hardiksatasiya_sotest_events_events', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->integer('events_id')->unsigned();
            $table->integer('related_events_id')->unsigned();
        });
    }

    public function down()
    {
        Schema::dropIfExists('hardiksatasiya_sotest_events_events');
    }
}

然后,就需要我们有正确的选项.

Then, just we need to add relation with proper options

public $belongsToMany =[
    'skills' => [
        'HardikSatasiya\SoTest\Models\Skills',
        'table' => 'hardiksatasiya_sotest_events_skills',
        'order' => 'title'
    ],
    'related_events' => [
        'HardikSatasiya\SoTest\Models\Events',
        'table' => 'hardiksatasiya_sotest_events_events',
        'order' => 'title',
        'key' => 'events_id',
        'otherKey' => 'related_events_id'
    ]
];

我们为此关系指定event_id是主事件,而related_events_id是其他事件,我们将其添加为相关事件. config_relation.yaml<--将位于事件控制器视图文件夹中.

we are specifying that for this relation event_id is for main event and related_events_id is for other event which we add as related event. config_relation.yaml <- which will be inside event controller view folder.

# ===================================
#  Relation Behavior Config
# ===================================

skills:
    label: Skill
    view:
        list: $/hardiksatasiya/sotest/models/skills/columns.yaml
        toolbarButtons: add|remove
    manage:
        list: $/hardiksatasiya/sotest/models/skills/columns.yaml
        form: $/hardiksatasiya/sotest/models/skills/fields.yaml

related_events:
    label: Events
    view:
        list: $/hardiksatasiya/sotest/models/events/columns.yaml
        toolbarButtons: add|remove
    manage:
        list: $/hardiksatasiya/sotest/models/events/columns.yaml
        form: $/hardiksatasiya/sotest/models/events/fields.yaml

事件模式字段$/hardiksatasiya/sotest/models/events/fields.yaml

fields:
    title:
        label: Title
        span: auto
        type: text
    description:
        label: Description
        size: ''
        span: auto
        type: textarea
    skills:
        label: Skills
        type: partial
        path: $/hardiksatasiya/sotest/controllers/events/_relation_skills.htm
    related_events:
        label: Events
        type: partial
        path: $/hardiksatasiya/sotest/controllers/events/_related_events.htm

现在表示部分_related_events.htm

<?= $this->relationRender('related_events') ?>

结果

使用代码访问

accessing in code

use HardikSatasiya\SoTest\Models\Events as EventsModel;

function onStart() {
    $this['event'] = $event = EventsModel::first();
    dd($event->related_events);
}

如有疑问,请发表评论.

if any doubt please comment.

这篇关于十月CMS与自我的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆