Laravel 4,来自一个模型的多个多态关系 [英] Laravel 4, Multiple Polymorphic Relations from one Model
问题描述
我试图在Laravel 4中建立多态关系,以便我可以拥有一个Image类来处理与上载,取消链接等有关的所有事情,然后将其用于多个不同的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. 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
Models/Image.php
class Image extends Eloquent {
public function of() { return $this->morphTo(); }
}
Models/Person.php
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
Models/Place.php
class Place extends Eloquent {
public function photos() { return $this->morphMany('Image', 'of'); }
}
此处,一个人可以上传一个mugshot
和许多photos
,而一个地方可以具有许多photos
.问题是,当我在Person上创建mugshot
时,它会将其保存到数据库的images
表中:
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
它没有存储它是mugshot
而不是photo
的事实,因此当我去检索它时,$person->mugshot
有时可能返回$person->photos
之一,反之亦然.
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.
有没有(a)比在同一模型上创建2个链接更好的方法,或者(b)实际上使这种方法起作用的方法?
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?
推荐答案
目前没有内置方法.也许在Laravel 4.1中应该完全重写多态关系.
No built-in way right now. Maybe in Laravel 4.1 that's supposed to bring a complete rewrite of polymorphic relations.
向Image
添加type
属性,然后在关系上定义where
条件:
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');
}
不要忘记在创建的Image
上设置type
.
或者,就像我下面一样,将这种逻辑隐藏在模型中.
Don't forget to set type
on Image
s you create.
Or, like I did bellow, hide that logic inside the model.
这是我的代码(我正在使用带有短数组表示法的PHP 5.4):
Here's my code (I'm using PHP 5.4 with short array notation):
图片:
namespace SP\Models;
class Image extends BaseModel {
const MUGSHOT = 'mugshot';
const PHOTO = 'photo';
protected $hidden = ['type'];
public function of()
{
return $this->morphTo();
}
}
人员:
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]);
这篇关于Laravel 4,来自一个模型的多个多态关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!