0

我有一个 laravel 模型

class Project extends Eloquent {

    public static $timestamps = true;

    public $includes = array('members','members.memberdata');

    public function tasks() {

        return $this->has_many('Usertask','project_id');
    }

    public function members() {

        return $this->has_many('Projectmember','project_id');
    }
}

和相关模型

class Projectmember extends Eloquent {

    public static $table = "project_members";
    public static $timestamps = true;

    public function project() {

        return $this->belongs_to('Project');
    }

    public function memberdata() {

        return $this->has_one('Usermetadata','user_id');
    }
}

class Usermetadata extends Eloquent {

    public static $table = "users_metadata";

    public function user() {

        return $this->belongs_to('User');
    }

    public function member() {

        return $this->belongs_to('Projectmember','user_id');
    }
}

当我尝试像这样检索单个项目模型时

 $project = Project::find($id);

 return Response::eloquent($project);

我的 json 输出看起来像这样

{"id":1,"user_id":1,"name":"UberWork","description":"Web based project management \/ task management app","target_date":"2013-11-15 00:00:00","budget":null,"status":0,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","members":[{"id":1,"project_id":1,"user_id":1,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":{"user_id":1,"first_name":"Tamarakuro","last_name":"Foh","photo":"","company_name":null,"phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"12b14a7d3ca48c53bb5b1a88fa3eca3b"}},{"id":3,"project_id":1,"user_id":3,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":{"user_id":3,"first_name":"Ebere","last_name":"Uche","photo":"","company_name":"Chronotech Labs","phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"ab446bd9ffbb898e818a892c7401e0f6"}},{"id":4,"project_id":1,"user_id":2,"created_at":"2013-04-17 08:13:00","updated_at":"2013-04-17 08:13:00","memberdata":null}]}

我的数据库看起来像这样;

用户

  • ID
  • 电子邮件
  • 密码
  • IP地址
  • 积极的
  • ...

用户元数据

  • ID
  • 用户身份
  • 头像照片
  • ...

项目

  • ID
  • 用户身份
  • 姓名
  • 描述
  • 地位
  • ...

项目成员

  • ID
  • 项目编号
  • 用户身份

我的问题是为什么项目的最后一个成员的 memberdata 为“null”,而其他成员不为 null。难道我做错了什么?

4

1 回答 1

0

Eloquent 中的关系总是将主键 (pk) 与外键 (fk) 联系起来。但是,您正在尝试基于两个外键建立关系,从而跳过了关系。唯一的 Eloquent 解决方案是包含额外的关系步骤。这里有一些模型(我已经省略了这个例子不需要的关系)......

class Project extends Eloquent {

    public static $timestamps = true;

    public $includes = array('members','members.user', 'members.user.metadata');

    public function members()
    {
        return $this->has_many('Projectmember','project_id');
    }
}

class Projectmember extends Eloquent {

    public static $table = "project_members";
    public static $timestamps = true;

    public function user()
    {
        return $this->belongs_to('User');
    }
}

class User extends Eloquent {

    public static $timestamps = true;

    public $hidden = array('password', 'ip_address');

    public function metadata()
    {
        return $this->has_one('Usermetadata');
    }

}

class Usermetadata extends Eloquent {

    public static $table = "users_metadata";

    public function user() {

        return $this->belongs_to('User');
    }

}

我明白你为什么要跳过这段关系,但遗憾的是,关系不可能。

于 2013-04-18T10:39:22.383 回答