Laravel 4, Multiple Polymorphic Relations from one Model

折月煮酒 提交于 2019-12-20 10:49:07

问题


I'm trying to set up polymorphic relationships in Laravel 4 so that I can have one Image class which handles everything related to uploads, unlinks and so on, then have it used by multiple different Models. That's fine, until I get to trying to create multiple links from the same Model.

For example, I currently have something like this:

Models/Image.php

class Image extends Eloquent {
    public function of() { return $this->morphTo(); }
}

Models/Person.php

class Person extends Eloquent {
    public function mugshot() { return $this->morphOne('Image', 'of'); }
    public function photos() { return $this->morphMany('Image', 'of'); }
}

Models/Place.php

class Place extends Eloquent {
    public function photos() { return $this->morphMany('Image', 'of'); }
}

Here, a Person can upload one mugshot and many photos, while a Place can have many photos. The problem is that when I create a mugshot on a Person, it saves this into the images table in the database:

id: 1
of_id: 1
of_type: Person

It doesn't store the fact that it's a mugshot and not a photo, so when I go to retrieve it, $person->mugshot may sometimes return one of $person->photos and vice versa.

Is there either (a) a better way to do this than creating 2 links on the same Model, or (b) a way to actually make this way work?


回答1:


No built-in way right now. Maybe in Laravel 4.1 that's supposed to bring a complete rewrite of polymorphic relations.

Add a type property to Image, then define where conditions on the relations:

public function mugshot() { 
    return $this->morphOne('Image', 'of')->where('type', 'mugshot'); 
}

public function photos() { 
    return $this->morphMany('Image', 'of')->where('type', 'photo'); 
}

Don't forget to set type on Images you create. Or, like I did bellow, hide that logic inside the model.

Here's my code (I'm using PHP 5.4 with short array notation):

Image:

namespace SP\Models;

class Image extends BaseModel {

    const MUGSHOT = 'mugshot';
    const PHOTO = 'photo';

    protected $hidden = ['type'];

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

Person:

namespace SP\Models;

use SP\Models\Image;

class Person extends BaseModel {

    public function mugshot() { 
        return $this->morphOne('SP\Models\Image', 'of')
                            ->where('type', Image::MUGSHOT); 
    }

    public function photos() { 
        return $this->morphMany('SP\Models\Image', 'of')
                            ->where('type', Image::PHOTO); 
    }

    public function saveMugshot($image)
    {
        $image->type = Image::MUGSHOT;
        $image->save();
        $this->mugshot()->save($image);
    }

    public function savePhotos($images)
    {
        if(!is_array($images))
        {
            $images = [$images];
        }

        foreach($images as $image)
        {
            $image->type = Image::PHOTO;
            $image->save();
            $this->photos()->save($image);
        }
    }
}

Somewhere in a controller/service:

$person->savePhotos([$image1, $image2]);


来源:https://stackoverflow.com/questions/19835996/laravel-4-multiple-polymorphic-relations-from-one-model

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