laravel Eloquent morphOne 关系不限于一种关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24694761/
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 09:48:12  来源:igfitidea点击:

Eloquent morphOne relationship doesn't limit to one relation

laravellaravel-4eloquentpolymorphism

提问by Keet

I am having a issue with an Eloquent morphOnerelationship where it is creating new entries rather than updating the one that already exists.

我遇到了 EloquentmorphOne关系的问题,它正在创建新条目而不是更新已经存在的条目。

Basically I have a number of models (for example, let's say Personand Building) that both need a location, so I have created a Locationmodel:

基本上我有许多模型(例如,假设PersonBuilding)都需要一个位置,所以我创建了一个Location模型:

class Location extends Eloquent {

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

}

Then in my other models I have this:

然后在我的其他模型中,我有这个:

class Person extends Eloquent {

    // ...

    /**
     * Get the person's location
     * 
     * @return Location
     */
    public function location()
    {
        return $this->morphOne('Location', 'locationable');
    }

    // ...
class Building extends Eloquent {

    // ...

    /**
     * Get the building's location
     * 
     * @return Location
     */
    public function location()
    {
        return $this->morphOne('Location', 'locationable');
    }

    // ...

When I run the following test code, it creates the location entry fine, but if I repeat it, it creates more entries.

当我运行以下测试代码时,它会很好地创建位置条目,但如果我重复它,它会创建更多条目。

$person = Person::first();

$loc = new Location;

$loc->lat = "123";
$loc->lng = "321";

$person->location()->save($loc);

Am I doing something wrong here? I would have expected morphOneto constrain this to one entry per type, so the last entry in the table below should not exist:

我在这里做错了吗?我本来希望将morphOne其限制为每种类型的一个条目,因此下表中的最后一个条目不应该存在:

+---------------------+--------------------------+
|  locationable_id    |   locationable_type      |
+---------------------+--------------------------+
|  2                  |  Building                |
|  3                  |  Building                |
|  2                  |  Person                  |
|  2                  |  Building                |
+---------------------+--------------------------+

回答by Murwa

Maybe I'm late for the party, but this worked for me!

也许我参加聚会迟到了,但这对我有用!

# Do we have location already?
if($person->location) {
    return $person->location()->update($location);
}
return $person->location()->create($location);

回答by Assarte

Based on my very current experiences, this solution should be works

根据我目前的经验,这个解决方案应该有效

Creation or replace old by new Location model instance:

用新的 Location 模型实例创建或替换旧的:

$person = Person::first();

$loc = new Location;

$loc->lat = "123";
$loc->lng = "321";

$loc->save();
$person->location()->associate($loc);
$person->save();

Update:

更新:

$person = Person::first();

$person->location->lat = "123";
$person->location->lng = "321";

$person->location->save();
$person->save();

I spent many days for finding out this solution. Hope they would document it much better officially.

我花了很多天来找出这个解决方案。希望他们能更好地正式记录它。

回答by sisou

Actually, by calling

实际上,通过调用

$loc = new Location;

you are, by definition, creating a new location!

根据定义,您正在创建一个新位置!

Calling

打电话

$person->location()->save($loc);

doesn't help either.

也没有帮助。

IF you want to updatea location, you need to find the location, update it's values and then save it. Independently from the parent model:

如果你想更新一个位置,你需要找到这个位置,更新它的值然后保存它。独立于父模型:

$person = Person::first();

$loc = $person->location;

$loc->lat = "123";
$loc->lng = "321";

$loc->save();

Done.

完毕。