多对多的多态关系 [英] many-to-many polymorphic relations

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

问题描述

我需要在 Entity2、Entity1 和 Types 之间创建多态关系.event和Types的关系很容易搞定,但是Entity2和Types的关系有问题,因为是多对多的关系.

I need to create a polymorphic relationship between Entity2, Entity1 and Types. the relationship between event and Types is easy to do, but tou have a problem in the relationship between Entity2 and Types, because it is a many-to-many relation.

class CreateTypesTable extends Migration {
   public function up()
    {
        Schema::create('types', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('typeable_id');
            $table->string('typeable_type', 20);
            $table->string('name', 20);
            $table->text('description')->nullable();
        });
    }
}

class Entity1 extends Eloquent {
    public function type()
    {
        return $this->morphMany('AppModelsType', 'typeable');
    }
}

class Type extends Eloquent {

    public function typeable()
    {
        return $this->morphTo();
    }
}

由于types和Entitys2的关系是多对多的,不知道怎么创建,因为需要一个pivot表.

as the relationship between types and Entitys2 is many to many, do not know how to create, because it takes a pivot table.

    class Entity2 extends Eloquent {
        public function types()
        {
//          return $this->morphMany('AppModelsType', 'typeable');
        }
    }

推荐答案

对于多对多关系,您需要使用另一种多态方法:https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L805 morhpToManybelongsToMany 在其中将相关模型作为参数传递.

You need to use another polymorphic method for a many-to-many relationship: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L805 morhpToMany and belongsToMany where you pass the related model as a parameter.

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

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