简体   繁体   English

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

[英]Laravel 4, Multiple Polymorphic Relations from one Model

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. 我正在尝试在Laravel 4中设置多态关系,以便我可以拥有一个Image类来处理与上传,取消链接等相关的所有内容,然后由多个不同的模型使用它。 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 型号/ 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 mugshot and many photos , while a Place can have many photos . 在这里,一个人可以上传一张mugshot和许多photos ,而一个地方可以有很多photos The problem is that when I create a mugshot on a Person, it saves this into the images table in the database: 问题是,当我在Person上创建一个mugshot时,它会将其保存到数据库中的images表中:

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. 它没有存储它是一个mugshot而不是photo的事实,所以当我去取回它时, $person->mugshot有时会返回$person->photos $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)实际使这种方式有效的方法?

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 type property to Image , then define where conditions on the relations: a添加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 type on Image s you create. 不要忘记在您创建的Image上设置type 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): 这是我的代码(我使用的是带有短数组符号的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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM