2014-10-28 48 views
1

我有两个表,schedulesshifts,具有一对多的关系。从关系密切的表中获取N行与关系密切的关系表上的条件

schedule具有daymonthyear字段,以及一个is_published布尔值。 A shift有一个user_idusers有一对一的关系。

现在,我想要获得user的下一个5即将到来的shifts。我必须从schedule开始,因为该表中有日期。但是,更重要的是,我应该只检索属于已发布的scheduleshifts

所以最大的问题是:

条目数应该是5个班次。然而,从时间表开始,我不知道我需要检索多少个时间表,直到有5个班次属于用户。

除了试验和错误(即检索x个下一个时间表和测试以查看是否存在足够的班次,如果不存在,则检索下n个时间表直到满足配额),是否有其他选择?

附表

Schema::create('schedules', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->integer('user_id', false, true); 
    $table->integer('client_id', false, true); 
    $table->datetime('for'); 
    $table->enum('type', array('template', 'revision', 'common')); 
    $table->string('name', 50)->default('Untitled Template'); 
    $table->boolean('is_published'); 
    $table->timestamp('published_at'); 
    $table->softDeletes(); 
    $table->timestamps(); 
}); 

这里的架构,这些user_id是创作者的ID,而不是谁的时间表属于。这用于跟踪未来如何创建时间表。

移位

Schema::create('shifts', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->integer('schedule_id', false, true); 
    $table->integer('user_id', false, true); 
    $table->foreign('schedule_id')->references('id')->on('schedules')->onDelete('cascade'); 
    $table->softDeletes(); 
    $table->timestamps(); 
}); 
+0

您是否在您的Eloquent模型中设置了关系? – lukasgeiter 2014-10-28 23:06:25

+0

是的,关系在Eloquent表中设置。我的问题不是如何链接关系,而是如何获得一定数量的“班次”,但从“schedule”开始 – Kousha 2014-10-28 23:14:46

+0

我明白了,只是确保...但我仍然有一个问题。你写道:“移位”与“用户”有一对一的关系。另一方面,你说你想要即将到来的5班属于用户。你最终希望在结果中安排时间表吗? – lukasgeiter 2014-10-28 23:18:20

回答

0

我会去简单的方法:

// User model 
public function shifts() 
{ 
    return $this->hasManyThrough('Shift', 'Schedule'); 
} 

则:

$now = Carbon\Carbon::now(); 

$upcomingShifts = $user->shifts() 
    ->where('schedules.is_published', 1) 
    ->where('schedules.for', '>', $now) 
    ->orderBy('schedules.for') 
    ->take(5) 
    ->get(); 

你可以把它多一点灵活的,如。这样:

// User model 
public function getUpcomingShifts($limit = 5) 
{ 
    $joinTable = $this->shifts()->getParent()->getTable(); // schedules 

    $now = Carbon\Carbon::now(); 

    return $this->shifts() 
    ->where($joinTable.'.is_published', 1) 
    ->where($joinTable.'.for', '>', $now) 
    ->orderBy($joinTable.'.for') 
    ->take($limit) 
    ->get(); 
} 

// then 
$user->getUpcomingShifts(); 
0

SELECT user.name,inner_q的架构。* FROM用户, (SELECT sch.year,sch.month,sch.day,sh.start_time FROM时间表SCH INNER JOIN shift sh ON sch.id = sh.schedule_id WHERE sch.is_published = TRUE和sh.user_id = user。id LIMIT 5)AS inner_q;

0

好吧,我不知道如果我得到这一切的权利,但这给一试:

User::find($userId) 
    ->shifts() 
    ->with('schedule') 
    ->where('is_published', true) 
    ->orderBy('year', 'asc') 
    ->orderBy('month', 'asc') 
    ->orderBy('day', 'asc') 
    ->take(5) 
    ->get(); 

更新

第二个尝试,希望这个作品

User::find($userId) 
    ->schedules() 
    ->has('shifts') 
    ->where('is_published', true) 
    ->orderBy('year', 'asc') 
    ->orderBy('month', 'asc') 
    ->orderBy('day', 'asc') 
    ->take(5) 
    ->get(); 

另一个更新

第三次是一个魅力:)

$results = User::find($userId) 
    ->schedules() 
    ->where('is_published', true) 
    ->orderBy('year', 'asc') 
    ->orderBy('month', 'asc') 
    ->orderBy('day', 'asc') 
    ->take(5) 
    ->get(); 

对于这个工作,你需要这个在您的用户模式:

public function schedules(){ 
    return $this->belongsToMany('Schedule', 'shifts'); 
} 

所以再访问就可以使用旋转属性的转变:

foreach($results as $result){ 
    echo $result->pivot 
} 

如果您想要完整的Shift模型而不是仅属性,请将其定义为枢轴模型:

class Shift extends Illuminate\Database\Eloquent\Relations\Pivot 

,并在进度模型:

public function newPivot(Eloquent $parent, array $attributes, $table, $exists){ 
    if ($parent instanceof User) { 
     return new Shift($parent, $attributes, $table, $exists); 
    } 
    return parent::newPivot($parent, $attributes, $table, $exists); 
} 
+0

感谢这篇文章,但正如我所说的,'日期','月份','年份'和'is_published'是'日程表'表格的一部分,而不是“移位”表格。这正是我的问题。我想'拿(5)',但是,这个操作是在'schedule'上调用的,而不是'shift'。 – Kousha 2014-10-28 23:37:41

+0

@Kousha好吧,你可能会发布你的数据库和你的模型的SQL脚本?我想尝试一些东西 – lukasgeiter 2014-10-28 23:40:28

+0

当然,你想要我的模式? – Kousha 2014-10-28 23:53:08