【问题标题】:Laravel models with multiple relationships not acting as expected具有多个关系的 Laravel 模型未按预期运行
【发布时间】:2014-09-21 10:46:26
【问题描述】:

我有一个User 模型和一个Organization 模型,我试图将它们相互关联。

users 表有 idcurrent_organization_id(外键)字段(在其他普通字段中)。

organizations 表具有 idowner_id(外键)字段(以及一些其他数据字段)。

还有一个数据透视表organization_user,通过它们各自的id 将两者联系起来。

我的模型是这样设置的:

用户:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {

  use UserTrait, RemindableTrait;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = array('password', 'remember_token');

  /**
   * Defining many to many relationship to organizations
   */
  public function organizations()
  {
    return $this->belongsToMany('Organization');
  }

  /**
   * Defining relationship to current selected organization
   */
  public function current_organization()
  {
    return $this->hasOne('Organization');
  }

}

组织:

<?php

class Organization extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];

    // Don't forget to fill this array
    protected $fillable = [];

  public function users()
  {
    return $this->hasMany('User');
  }

  public function owner()
  {
    return $this->belongsTo('User');
  }
}

这个想法是单个组织归一个用户所有,但一个组织有很多用户,每个用户都有一个“当前”组织,他们可以从他们所属的任何组织中选择。

我遇到的问题是,当我尝试执行$user-&gt;current_organization-&gt;id 时,我得到一个Trying to get property of non-object 错误,如果我尝试$user-&gt;current_organization()-&gt;id;,我得到一个Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$id 错误。

关于我做错了什么的任何想法,我无法像我在上面尝试做的那样检索current_organization

编辑:

我认为这与我的 hasOne 关系有关,但我尝试这样做:

public function current_organization()
{
  return $this->hasOne('Organization', 'id', 'current_organization_id');
}

还是什么都没有。 $user-&gt;current_organization; 正在返回 NULL

【问题讨论】:

    标签: php laravel laravel-4 eloquent


    【解决方案1】:

    您需要为当前组织使用belongsTo 关系。

    public function current_organization()
    {
        return $this->belongsTo('Organization', 'current_organization_id');
    }
    

    【讨论】:

    • 不幸的是它仍然返回NULL。我再次检查以确保$user-&gt;current_organization_id 正在重新调整1Organization::find($user-&gt;current_organization_id) 正在返回正确的组织。还有其他想法吗?
    【解决方案2】:

    经过大量调试和搜索,我遇到了this post,这让我遇到了真正的问题——函数名中的下划线。

    来自链接:

    Eloquent 文档提到“请记住,方法应该遵循 骆驼大小写,即使你的数据库列是蛇形大小写。”所以我 相信函数名应该是“objectMinor()”。雄辩 实际上将蛇形列名转换为驼峰式 方法名称。这有点令人困惑,但它确实符合 PHP 命名约定。

    所以,使用@Cryode 回答和以上,我想出了:

    public function currentOrganization()
    {
      return $this->belongsTo('Organization', 'current_organization_id');
    }
    

    可以通过以下方式调用:

    $organization = $user->current_organization;
    

    我认为他们真的应该让这一点更加明显。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多