Eloquent morphOne relationship doesn't limit to one relation

╄→гoц情女王★ 提交于 2019-12-04 12:35:39

问题


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

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

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 morphOne to constrain this to one entry per type, so the last entry in the table below should not exist:

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

回答1:


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 update a 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.




回答2:


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



回答3:


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

Creation or replace old by new Location model instance:

$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.



来源:https://stackoverflow.com/questions/24694761/eloquent-morphone-relationship-doesnt-limit-to-one-relation

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