/**
  * Join the query with a subquery. Warning, in order to use this method
  * properly, the join must be executed at the start of the query. If
  * it's added t the end of the query, the bindings won't match up.
  *
  * @param  \October\Rain\Database\Builder   $query      Query being joined to
  * @param  \October\Rain\Database\Builder   $subquery   Subquery being joined
  * @param  string                           $alias      Joined table alias
  * @param  string                           $left       Left side of condition
  * @param  string                           $operator   Join condition operator
  * @param  string                           $right      Right side of condition
  * @param  string                           $join       Join type [ join, leftJoin ]
  * @return \October\Rain\Database\Builder
  */
 public function scopeJoinSubquery($query, $subquery, $alias, $left, $operator, $right, $join = 'join')
 {
     $self = $this->getTable() . '.*';
     if (!in_array($self, $query->getQuery()->columns)) {
         $query->addSelect($self);
     }
     $subquery = $subquery->getQuery();
     $raw = DB::raw('(' . $subquery->toSql() . ') ' . $alias);
     return $query->{$join}($raw, $left, $operator, $right)->mergeBindings($subquery);
 }
Exemple #2
0
 /**
  * Controller override: Extend the query used for populating the list
  * after the default query is processed.
  * @param \October\Rain\Database\Builder $query
  */
 public function listExtendQuery($query, $definition = null)
 {
     switch ($definition) {
         case 'archived':
             $query->where('archived', '=', true);
             break;
         default:
             $query->where('archived', '=', false);
             break;
     }
 }
 /**
  * Select an inventory by it's selection signature
  *
  * @param  \October\Rain\Database\Builder   $query
  * @param  array                            $selections
  * @return \October\Rain\Database\Builder
  */
 public function scopeWhereHasSelections($query, $selections)
 {
     return $query->where(function ($inventory) use($selections) {
         $inventory->has('selections', '=', count($selections));
         foreach ($selections as $id) {
             $inventory->whereHas('selections', function ($selection) use($id) {
                 $selection->where('id', $id);
             });
         }
     });
 }
 /**
  * This exists to makes statuses sortable by assigning them a value
  *
  * Expired  -1
  * Running  0
  * Upcoming 1
  *
  * @param  \October\Rain\Database\Builder   $query
  * @return \October\Rain\Database\Builder
  */
 public function scopeSelectStatus($query)
 {
     $grammar = $query->getQuery()->getGrammar();
     $start_at = $grammar->wrap($this->table . '.start_at');
     $end_at = $grammar->wrap($this->table . '.end_at');
     $now = Carbon::now();
     $subquery = "CASE " . "WHEN ({$end_at} IS NOT NULL AND {$end_at} < '{$now}') THEN -1 " . "WHEN ({$start_at} IS NOT NULL AND {$start_at} > '{$now}') THEN 1 " . "ELSE 0 " . "END";
     return $query->selectSubquery($subquery, 'status');
 }
Exemple #5
0
 /**
  * Select a campaign by CMS object
  *
  * @param   Builder     $query
  * @param   Form        $form
  * @return  Builder
  */
 public function scopeWhereCmsObject(Builder $query, Form $form)
 {
     if ($form->model instanceof Layout) {
         $type = 'layout';
     } elseif ($form->model instanceof Page) {
         $type = 'page';
     } elseif ($form->model instanceof Partial) {
         $type = 'partial';
     } else {
         $type = 'unknown';
     }
     return $query->where('file_type', $type)->where('file_name', $form->model->getFileName());
 }
 public function scopeCurrentMonth(Builder $query)
 {
     $query->where('month', date('m'))->where('year', date('Y'));
 }
Exemple #7
0
 /**
  * Selects discounted prices
  *
  * @param  \October\Rain\Database\Builder   $query
  * @return \October\Rain\Database\Builder
  */
 public function scopeIsDiscounted($query)
 {
     return $query->whereNotNull('discount_id');
 }
 /**
  * Scope for checking if model is enabled
  * @param  \October\Rain\Database\Builder $query
  * @return \October\Rain\Database\Builder
  */
 public function scopeIsEnabled($query)
 {
     return $query->whereNotNull('is_enabled')->where('is_enabled', true);
 }
 /**
  * Returns categories that are not filtered
  *
  * @param  \October\Rain\Database\Builder   $query
  * @return \October\Rain\Database\Builder
  */
 public function scopeIsNotFiltered($query)
 {
     return $query->doesntHave('filters');
 }
Exemple #10
0
 /**
  * Get the given macro by name.
  *
  * @param string $name
  * @return \Closure 
  * @static 
  */
 public static function getMacro($name)
 {
     //Method inherited from \Illuminate\Database\Eloquent\Builder
     return \October\Rain\Database\Builder::getMacro($name);
 }
 /**
  * Paginate resultset 
  * 
  * @param Builder $query
  * @return Response
  */
 protected function paginateResult(Builder $query)
 {
     try {
         $pageSize = $this->getPageSize();
         if ($pageSize > 0) {
             $paginator = $query->paginate($pageSize);
             return Response::api()->withPaginator($paginator, $this->getTransformer());
         } else {
             return Response::api()->withCollection($query->get(), $this->getTransformer());
         }
     } catch (\Exception $e) {
         $message = $e->getMessage();
         if ($e instanceof QueryException) {
             \Log::error('API endpoint ' . get_class($this) . ' : ' . $e->getMessage());
             $message = 'One or multiple filter fields does not exists in the model';
         }
         return Response::api()->errorInternalError($message);
     }
 }
 /**
  * This exists to makes statuses sortable by assigning them a value
  *
  * Disabled     -2
  * Out of stock -1
  * Normal        0
  * Discounted    1
  *
  * @param  \October\Rain\Database\Builder   $query
  * @return \October\Rain\Database\Builder
  */
 public function scopeSelectStatus($query)
 {
     $grammar = $query->getQuery()->getGrammar();
     $price = $grammar->wrap('price');
     $inventory = $grammar->wrap('inventory');
     $is_enabled = $grammar->wrap('bedard_shop_products.is_enabled');
     $base_price = $grammar->wrap('bedard_shop_products.base_price');
     $subquery = "CASE " . "WHEN {$is_enabled} = 0 THEN -2 " . "WHEN ({$inventory} IS NULL or {$inventory} = 0) THEN -1 " . "WHEN {$price} < {$base_price} THEN 1 " . "ELSE 0 " . "END";
     return $query->selectSubquery($subquery, 'status');
 }