Skip to content

Instantly share code, notes, and snippets.

@kapv89
Last active January 2, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kapv89/8260599 to your computer and use it in GitHub Desktop.
Save kapv89/8260599 to your computer and use it in GitHub Desktop.
reposed example
<?php
class Boxes extends k\Reposed\Repository
{
protected function leftJoinOrdersPivot()
{
return $this->newJoint(function ($q) {
$pivot = Order::repo()->orderedBoxes()->getTable();
$q->leftJoin(
$pivot,
$pivot.'.box_id', '=', $this->c('id')
);
});
}
protected function joinWorkflowStages()
{
return $this->newJoint(function ($q) {
$q->join(WorkflowStage::table(), $this->c('workflow_stage_id'), '=', WorkflowStage::c('id'));
});
}
protected function joinOrders()
{
return $this->joinOrdersPivot()->newJoint(function ($q) {
$pivot = Order::repo()->orderedBoxes()->getTable();
$q->join(Order::table(), Order::c('id'), '=', $pivot.'.order_id');
});
}
protected function joinOrdersWithCurrentState()
{
return $this->joinOrders()->newJoint(function ($q) {
$q->leftJoin(Order\State::table(), Order::c('current_state_id'), '=', Order\State::c('id'));
});
}
public function forWarehouse(Warehouse $warehouse = null)
{
return $this->newScoped(function ($q) use ($warehouse) {
if($warehouse) $q->where($this->c('warehouse_id'), '=', $warehouse->id);
else $q->whereNull($this->c('id'));
});
}
public function incomingToWorkflowStage(WorkflowStage $stage = null)
{
if($stage === null)
{
return $this->newScoped(function ($q) { $q->whereNull($this->c('id')); });
}
else
{
return $this->forWarehouse($stage->warehouse)
->joinOrdersWithCurrentState()
->joinWorkflowStages()
->newScoped(function ($q) use ($stage) {
$q->whereIn(Order::c('type'), Order::incomingTypes())
->whereIn(Order\State::c('name'), [Order\State::processing, Order\State::processed])
->where(WorkflowStage::c('weight'), '<=', $stage->weight);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment