drrhr20884 2014-07-29 22:14
浏览 35
已采纳

具有多种关系的Laravel模型未按预期运行

I have a User model and an Organization model which I'm trying to relate to one another.

The users table has id and current_organization_id (foreign key) fields (among the other normal fields).

The organizations table has id and owner_id (foreign key) fields (along with some other data fields).

There is also a pivot table, organization_user which links the two via their respective id.

My models are set up like this:

User:

<?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');
  }

}

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

The idea is that a single organization is owned by a user, but an organization has many users, and each user has a "current" organization, which they can select from any organization that they belong to.

The problem I'm running into is that when I try to do $user->current_organization->id I get a Trying to get property of non-object error, and if I try $user->current_organization()->id; I get a Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$id error.

Any ideas on what I'm doing wrong that I can't retrieve the current_organization like I'm trying to do above?

EDIT:

I'm thinking it has to do with my hasOne relationship, but I tried doing this:

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

and still nothing. $user->current_organization; is returning NULL.

  • 写回答

2条回答 默认 最新

  • dongyu7074 2014-07-31 14:24
    关注

    After much debugging and searching, I came across this post which led me to the real problem -- the underscore in the function name.

    From the link:

    The Eloquent docs mention "keep in mind that the methods should follow camel-casing, even though your database columns are snake-case." So I believe the function name should be "objectMinor()". Eloquent actually converts the snake-case column names to camel-case for the method names. This is a bit confusing, but it does it to conform to PHP naming conventions.

    So, using @Cryode answer and the above, I came up with:

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

    Which can be called via:

    $organization = $user->current_organization;
    

    I think they should really make this more obvious.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制