Laravel-4多态关系仅分配类型(不是Id)

Fir*_*iro 3 polymorphism polymorphic-associations laravel laravel-4

我试图与Laravel建立多态关系.我已经阅读了文档并查看了相关代码,但似乎无法完成所有工作.我想附加photosrooms一个房间可以有很多照片的地方.在type与关系工程有关,但id始终0.

为什么它的一部分会起作用,有人可以指出我的关系中有什么问题吗?

我认为问题与我在表格中创建数据的方式有关:

$photo = new Photo;
$photo->URL = 'thePhotoURL.extension';
$photo->save();

$room = new Room;
$room->name = 'Some Room Name';
// seems weird to me that I "save" the photo twice
// or am I just saving the relationship here?
// have tried the other relationship options too (associate, attach, synch)
$room->photos()->save($photo);
// have also tried:
// $room->photos()->save(new Photo(array('URL' => 'urlTest.com')));
// get error "URL"

$room->save();
Run Code Online (Sandbox Code Playgroud)

创建照片表:

public function up() {
    Schema::create('photos', function($t) {

        $t->increments('id');

        // ... ...
        // the Id is always 0 but the type is correct
        $t->integer('imageable_id');
        $t->string('imageable_type');

        $t->softDeletes();
        $t->timestamps();

    });
}
Run Code Online (Sandbox Code Playgroud)

我的班级,

照片:

class Photo extends Eloquent {
    public function imageable() {
        return $this->morphTo();
    }
}
Run Code Online (Sandbox Code Playgroud)

房间:

class Room extends Eloquent {       
    public function photos() {
        return $this->morphMany('Photo', 'imageable');
    }
}
Run Code Online (Sandbox Code Playgroud)

我看到了保留字(如事件)的问题,但这似乎不是问题.

wes*_*ide 8

$room->photos()->save($photo);保存原件后需要运行.原件尚不存在,因此没有要关联的ID.

也许它应该抛出异常,也许不是.就个人而言,我宁愿它尖叫着对我说.