雄辩的morphOne关系不限于一个关系 [英] Eloquent morphOne relationship doesn't limit to one relation

查看:290
本文介绍了雄辩的morphOne关系不限于一个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$

$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

基本上我有一些模型(例如,让我们说 Person Building )都需要一个位置,所以我创建了一个位置模型:

  class Location Enloquent {

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

}

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

  class Person extends Eloquent {

// ...

/ **
*获取该人的位置
*
* @return位置
* /
public function location()
{
return $ this-> morphOne('Location','locationable');
}

// ...





  class Building Enloquent {

// ...

/ **
*获取建筑物的位置
*
* @return位置
* /
public function location()
{
return $ this-> morphOne('Location' ,可定位);
}

// ...

当我运行以下测试代码,它创建位置条目罚款,但如果我重复,它会创建更多的条目。

  $ person =人::第一(); 

$ loc = new位置;

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

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

我在这里做错什么?我会预期 morphOne 将其限制为每个类型的一个条目,因此下表中的最后一个条目不应该存在:

  + --------------------- + -------------- ------------ + 
| locationable_id | locationable_type |
+ --------------------- + ----------------------- --- +
| 2 |建筑|
| 3 |建筑|
| 2 |人|
| 2 |建筑|
+ --------------------- + ----------------------- --- +


解决方案

实际上,通过调用

  $ loc = new Location; 

根据定义,您将创建一个新的位置!



调用

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

没有帮助。



如果要更新某个位置,则需要找到该位置,更新它的值,然后保存。独立于父模型:

  $ person = Person :: first(); 

$ loc = $ person-> location;

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

$ loc-> save();

完成。


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                |
+---------------------+--------------------------+

解决方案

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.

这篇关于雄辩的morphOne关系不限于一个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆