v5.5和v5.6之间的HasOne关系差异-不再相同了吗?

时间:2019-05-07 13:28:44

标签: laravel eloquent relationship

我有一个具有人口统计学信息的Person模型,一个具有组织信息的Orgs模型以及一个有效的带有附加字段的数据透视表的OrgPerson模型。我选择OrgPerson来扩展Model而不是Pivot,因为它在我的设置中效果更好。

error: conversion from ‘std::vector<U>’ to non-scalar type ‘std::vector<const U&, std::allocator<const U&> >’ requested

error: forming pointer to reference type ‘const U&

我承认OrgPerson模型是多对多关系的捕获。但是,我需要能够获取与defaultOrgID相关的人员的“组合人口统计”数据(存储在OrgPerson记录中)。

由于一个Person可以属于多个组织,因此我希望该人的orgperson记录,但要存储在$ person-> defaultOrgID中的“默认”组织的orgperson记录。

我的Person模型在v5.5中定义了以下关系,并通过$ person-> load('orgperson')或渴望的加载返回了我想要的东西。

REV %>% group_by(ID) %>%
  mutate(rev_latest = max(review_date)) %>%
  filter(rev_latest - review_date < 30) %>%
  count(ID)

运行代码,例如:

--- Edit: table structures ---
Table: person (Model: Person)
    PK: personID
        (other demographic fields)
        defaultOrgID (FK -> orgID) // the org that will be displayed/referenced by default 
                                   // changeable by user with multiple orgs

Table: organization (Model: Org)
    PK: orgID
        (other org-demographic fields)

Table: org-person (Model: OrgPerson -- functionally a pivot table)
    PK: personID, orgID
        (other combo-demographic fields for the person with respect to the org)

--- End of table structures edit ---

在关系列表中返回具有非null orgperson模型的Person模型。

升级到v5.6并执行了一些测试之后,我发现这种关系没有出现。在我的任何测试中,我都没有填充它,也找不到任何原因。

1 个答案:

答案 0 :(得分:0)

添加迁移以为orgperson表提供主键

Schema::table('orgperson', function (Blueprint $table) {
    $table->bigIncrements('id');
});

所以你可以做这样的事情

public function orgperson()
{
    return $this->hasOne(OrgPerson::class, 'defaultOrgID', 'personID')
}
相关问题