October CMS relation to self

浪子不回头ぞ 提交于 2020-02-29 07:22:05

问题


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

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?

Thanks in advance.


回答1:


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'
    ]
];

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

event modal fields $/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

now for partial _related_events.htm

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

result

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.



来源:https://stackoverflow.com/questions/52961104/october-cms-relation-to-self

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!