【发布时间】:2013-06-19 18:09:09
【问题描述】:
我需要在 Entity2、Entity1 和 Types 之间创建多态关系。 event 和 Types 的关系很容易做,但是 Entity2 和 Types 的关系有问题,因为它是多对多的关系。
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('App\Models\Type', 'typeable');
}
}
class Type extends Eloquent {
public function typeable()
{
return $this->morphTo();
}
}
由于types和Entity2的关系是多对多的,不知道怎么创建,因为它需要一个数据透视表。
class Entity2 extends Eloquent {
public function types()
{
// return $this->morphMany('App\Models\Type', 'typeable');
}
}
【问题讨论】:
标签: laravel laravel-4 polymorphic-associations eloquent