Eloquent模型关系返回null

时间:2013-04-17 18:21:07

标签: php laravel eloquent

我有一个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_ADDRESS
  • 活性
  • ...

users_metadata

  • ID
  • USER_ID
  • 如first_name
  • 姓氏
  • profile_photo
  • ...

项目

  • ID
  • USER_ID
  • 名称
  • 描述
  • 状态
  • ...

project_members

  • ID
  • PROJECT_ID
  • USER_ID

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

1 个答案:

答案 0 :(得分: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');
    }

}

我可以理解为什么你想跳过这段关系,但遗憾的是,关系并不可能。