【发布时间】:2013-10-08 03:19:42
【问题描述】:
所以我正在尝试创建一个taggable 表,它使多个models 具有与之关联的标签,并认为Laravel 的Polymorphic relationships 将是要走的路。
不幸的是,我似乎无法让它们在以下设置中工作。因为我在运行php artisan migrate:refresh --seed 时收到以下错误。
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class name must be a valid object or a string","file":"...\\vendor\\laravel\\framework\\src\\
Illuminate\\Database\\Eloquent\\Model.php","line":527}}
我认为问题是由于Taggable 模型具有相同的morphTo,如下所述。因为改变这个解决了这个问题。为什么这会导致问题?
可标记模型
class Taggable extends Eloquent {
protected $table = 'taggable';
public function taggable()
{
return $this->morphTo();
}
}
跟踪模型
class Track extends Eloquent {
protected $table = 'tracks';
protected $fillable = array('title', 'year', 'image');
protected $guarded = array('id');
public function playlists()
{
return $this->belongsToMany('Playlist');
}
public function tags()
{
return $this->morphMany('Taggable', 'taggable');
}
}
标签模型
class Tag extends Eloquent {
protected $table = 'tags';
protected $fillable = array('title', 'description');
protected $guarded = array('id');
}
迁移
Schema::create('taggable', function(Blueprint $table)
{
$table->increments('id');
$table->integer('taggable_id');
$table->string('taggable_type');
$table->integer('tag_id');
$table->timestamps();
});
DatabaseSeeder Snippit
...
DB::table('taggable')->delete();
$track1 = Track::find(1);
$idm = Tag::find(1);
$track1->tags()->create(array('tag_id' => $idm->id));
...
在此问题上的任何帮助将不胜感激。
【问题讨论】:
标签: php orm laravel laravel-4 polymorphic-associations