十月CMS与自我的关系

时间:2018-10-24 04:25:31

标签: octobercms

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

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

谢谢。

1 个答案:

答案 0 :(得分:2)

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

您可以像这样创建中间表

<?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');
    }
}

enter image description here

  

然后,我们只需要添加带有适当选项的关系

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 <-将位于事件控制器视图文件夹中。

# ===================================
#  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') ?>
  

结果

enter image description here

  

使用代码访问

use HardikSatasiya\SoTest\Models\Events as EventsModel;

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

如有疑问,请发表评论。