Laravel 4,来自一个模型的多个多态关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19835996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:38:20  来源:igfitidea点击:

Laravel 4, Multiple Polymorphic Relations from one Model

phplaravelpolymorphism

提问by Joe

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.

我正在尝试在 Laravel 4 中设置多态关系,以便我可以拥有一个 Image 类来处理与上传、取消链接等相关的所有内容,然后让多个不同的模型使用它。没关系,直到我开始尝试从同一个模型创建多个链接。

For example, I currently have something like this:

例如,我目前有这样的事情:

Models/Image.php

模型/Image.php

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

Models/Person.php

模型/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

模型/Place.php

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

Here, a Person can upload one mugshotand many photos, while a Place can have many photos. The problem is that when I create a mugshoton a Person, it saves this into the imagestable in the database:

在这里,一个 Person 可以上传一个mugshot和多个photos,而一个 Place 可以上传多个photos。问题是,当我mugshot在 Person 上创建一个时,它会将其保存到images数据库中的表中:

id: 1
of_id: 1
of_type: Person

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

它不存储它是 amugshot而不是 a的事实photo,所以当我去检索它时,$person->mugshot有时可能会返回其中之一$person->photos,反之亦然。

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?

是否有 (a) 比在同一模型上创建 2 个链接更好的方法,或者 (b) 一种实际使这种方式起作用的方法?

回答by Sergiu Paraschiv

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

目前没有内置方式。也许在 Laravel 4.1 中,这应该会带来对多态关系的完全重写。

Add a typeproperty to Image, then define whereconditions on the relations:

添加一个type属性到Image,然后定义where关系的条件:

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 typeon Images you create. Or, like I did bellow, hide that logic inside the model.

不要忘记typeImage您创建的 s上设置。或者,就像我在下面所做的那样,将该逻辑隐藏在模型中。

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

这是我的代码(我使用的是带有短数组符号的 PHP 5.4):

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]);