【问题标题】:Laravel 4 relationship and eventsLaravel 4 关系和事件
【发布时间】:2014-05-22 10:42:20
【问题描述】:

我在 Eloquent 关系方面遇到问题。关系是......每个用户都可以有一个所有者,它也是一个用户。

当我尝试使用 Parent 获取用户时:

在 *nix OS 和 PHP 5.4.20 上,我得到与 Parent 相同的 User,因此 parent 和 user 都是相同的。

而在 PHP5.4.7(如果重要的话,Win 7)上,它返回正确的数据。顺便说一句,这段代码是某个事件的事件处理程序。

用户模型

class User extends Eloquent implements UserInterface, RemindableInterface, PresentableInterface {
  protected $fillable = array(
    'first_name', 'last_name', 'email', 'password', 're_type_password', 'birth_date',
    'phone', 'address', 'state', 'city', 'zip', 'profile_pic', 'owner_id', 'can_edit_appointments',
    'can_accept_invitations', 'can_edit_profile', 'can_receive_notification', 'is_active', 'is_admin', 'token', 'failed_login_attempts_count'
  );

  public function __construct($validator = null, $subAccountValidator = null)
  {
    parent::__construct();
    $this->validator = $validator ?: App::make('ReminderMillie\Services\Validators\UserValidator');
    $this->subAccountValidator = $subAccountValidator ?: App::make('ReminderMillie\Services\Validators\SubAccountValidator');
  }


  public function getDates()
  {
      return array('created_at', 'updated_at', 'birth_date');
  }


  /**
   *  Relations
   */
  public function business()
  {
    return $this->hasOne('Business', 'owner_id');
  }

  public function parent()
  {
    return $this->belongsTo('User', 'owner_id');
  }

  public function subAccounts()
  {
    return $this->hasMany('User', 'owner_id');
  }

}    

【问题讨论】:

  • 你能和我们分享User模型吗?
  • 能否分享一下查询关系的代码?该模型看起来不错
  • 当然。 laravel.io/bin/JyLdo 问题在线#53
  • 我看不出有什么理由这样做。两台机器(*nix 和 windows 7 机器)之间数据库中的数据是否相同?

标签: laravel laravel-4 eloquent


【解决方案1】:

我认为你的逆是不正确的......

请检查一下

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

public function subAccounts()
{
    return $this->hasMany('User', 'id', 'owner_id');
}

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2015-10-25
    • 2015-01-01
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多