雄辩的过滤集合与渴望加载

时间:2015-03-09 22:14:44

标签: php orm eloquent

我一直在网上看,似乎无法解决这个问题。 我有两个表:Teams和TeamMembers表。一个团队可以拥有一个或多个团队成员,因此这是一对多的关系。

以下是两个表格的基本内容:

   class Team extends Base\EloquentBase
       {
          // Relationship
          public function teamMembers()
          {
            return $this->hasMany('TeamClock\Eloquent\Models\TeamMember', 'team_id', 'team_id');
          }
       }

       class TeamMember extends Base\EloquentBase
       {
          // Relationship
          public function team()
          {
            return $this->belongsTo('TeamClock\Eloquent\Models\Team', 'team_id', 'team_id');

          // Scope
          public function scopeHasDailyPerDiem($q)
          {
            return $q->where('is_daily_per_diem', '>', 0);
          }
       }

正如您所看到的,我在TeamMember课程中有一个范围,我希望所有拥有“is_daily_per_diem”列的团队成员的团队都大于0。

我有一个测试控制器,我正在抛弃结果:

$teams = Team::has('teamMembers')->with(array('teamMembers' => function($query){
                $query->hasDailyPerDiem();
        }))->get();

这是我在倾销时得到的:

[0] => Array
        (
            [team_id] => 55
            [phase_id] => value
            [team_name] => value
            [team_member_quota] => value
            [team_member_overstaff] => value
            [published] => value
            [tools_required] => value
            [dress_code] => value
            [special_instructions] => value 
            [team_members] => Array
                (
                )

        )
...
[41] => Array
        (
            [team_id] => 115
            [phase_id] => value
            [team_name] => value
            [team_member_quota] => value
            [team_member_overstaff] => 0
            [published] => 1
            [tools_required] => 
            [dress_code] => 
            [special_instructions] => 
            [team_members] => Array
                (
                    [0] => Array
                        (
                            [team_member_id] => value
                            [team_id] => 115
                            [position_id] => value
                            [employee_id] => value
                            [team_auth_id] => 1
                            [progress_auth_id] => 2
                            [is_daily_per_diem] => 1
                            [daily_per_diem] => 60.00
                            [pay_type_id] => 1
                            [flat_rate] => 
                            [is_active] => 0
                            [per_diem_beg_date] => 
                            [per_diem_end_date] => 
                            [beg_shift_id] => 
                            [end_shift_id] => 
                            [assign_email_guid] => value
                        )

                )

        )

正如你所看到的,我将团队成员(每日每日津贴数为0)和团队成员空缺的团队混合在一起。

有没有办法消除空团队成员的团队?

我也在我的测试控制器中试过这个和其他东西:

$teams = Team::whereHas('teamMembers', function($query){
            $query->hasDailyPerDiem();
        })->get(); 

但这会返回所有团队成员的数组,其中“每日每日死亡人数”大于零,但我没有得到任何团队信息:

[0] => Array
        (
            [team_id] => 55
            [phase_id] => value
            [team_name] => value
            [team_member_quota] => value
            [team_member_overstaff] => value
            [published] => value
            [tools_required] => value
            [dress_code] => value
            [special_instructions] => value
        )
...
 [36] => Array
        (
            [team_id] => 115
            [phase_id] => value
            [team_name] => value
            [team_member_quota] => value
            [team_member_overstaff] => value
            [published] => value
            [tools_required] => value
            [dress_code] => value
            [special_instructions] => value
        )

非常感谢任何帮助!!!

2 个答案:

答案 0 :(得分:1)

你差不多......

如果您想过滤团队和热切的加载成员:

$teams = Team::with('teamMembers')->whereHas('teamMembers', function($query){
             $query->hasDailyPerDiem();
         })->get(); 

如果你想过滤团队和渴望加载的成员:

$teams = Team::with(['teamMembers' => function($query){
             $query->hasDailyPerDiem();
         }])->whereHas('teamMembers', function($query){
             $query->hasDailyPerDiem();
         })->get();

如果要使用第二个代码块减少重复代码,请将过滤器闭包放在变量中:

$filter = function($query){
    $query->hasDailyPerDiem();
};

$teams = Team::with(['teamMembers' => $filter])
             ->whereHas('teamMembers', $filter)
             ->get();

答案 1 :(得分:0)

定义一个实现Scope接口的类

TeamFilterScope类实现范围{

     public function apply(Builder $builder, Model $model){
     $builder->hasDailyPerDiem()
    }
}

在模特的靴子中

protected function static boot(){
    parent::boot();
    static::addGlobalScope(new TeamFilterScope());
}