/** * Method for retrieving multiple records */ public function read_multiple() { if (!$this->valid_options()) { return $this; } // Multilanguage, lets check if we need to join the records (for search or sort) if ($this->multilanguage) { $filters = $this->get_filters($this->language_table); // Find out if we need a join if ($this->needs_join($this->language_table) || $this->versioned) { $table = $this->table; $language_table = $this->language_table; $language_table_foreign_key = $this->language_table_foreign_key; // Add a join for the language table $this->joins[] = array('table' => $language_table, 'join' => array(function ($join) use($table, $language_table, $language_table_foreign_key, $filters) { foreach ($filters as $filter) { list($table, $column, $value) = $filter; $join->where($table . '.' . $column, '=', $value); } $join->on($table . '.id', '=', $language_table . '.' . $language_table_foreign_key); $join->on($language_table . '.language_id', '=', 1); })); if ($this->versioned) { // Add a left outer join to be able to only get // the language rows at the latest version $this->joins[] = array('table' => $language_table . ' AS max', 'join' => array(function ($join) use($table, $language_table, $language_table_foreign_key, $filters) { foreach ($filters as $filter) { list($table, $column, $value) = $filter; $join->where($table . '.' . $column, '=', $value); } $join->on($table . '.id', '=', 'max.' . $language_table_foreign_key); $join->on('max.version', '>', $language_table . '.version'); $join->on('max.language_id', '=', 1); }, '', '', 'LEFT OUTER')); $this->get_columns[] = $language_table . '.version'; $this->model = $this->model->where('max.version', 'IS', DB::raw('NULL')); } } else { $this->model->includes[] = array('lang' => function ($query) use($filters) { foreach ($filters as $filter) { list($table, $column, $value) = $filter; $query = $query->where($table . '.' . $column, '=', $value); } }); } } elseif ($this->versioned) { $this->model = $this->model->where('version', '=', DB::raw('(' . DB::table($this->table . ' AS max')->select(DB::raw('MAX(version) AS version'))->where($this->table . '.id', '=', DB::raw('max.id'))->sql() . ')')); } $this->apply_joins(); foreach ($this->get_filters($this->table) as $filter) { list($table, $column, $value) = $filter; $this->model = $this->model->where($table . '.' . $column, '=', $value); } // Add where's to our query if ($this->options['search']['string'] !== '') { $columns = $this->options['search']['columns']; $string = $this->options['search']['string']; $this->model = $this->model->where(function ($query) use($columns, $string) { foreach ($columns as $column) { $query->or_where($column, ' LIKE ', '%' . $string . '%'); } }); } $total = (int) $this->model->count(); // Add order_by, skip & take to our results query $results = $this->model->order_by($this->options['sort_by'], $this->options['order'])->skip($this->options['offset'])->take($this->options['limit'])->get($this->get_columns); $this->data = array('results' => to_array($results), 'total' => $total, 'pages' => ceil($total / $this->options['limit'])); $this->apply_mappings(); return $this; }
/** * Adjust the value of a column up or down by a given amount. * * @param string $column * @param int $amount * @param string $operator * @return int */ protected function adjust($column, $amount, $operator) { $wrapped = $this->grammar->wrap($column); // To make the adjustment to the column, we'll wrap the expression in an // Expression instance, which forces the adjustment to be injected into // the query as a string instead of bound. $value = Database::raw($wrapped . $operator . $amount); return $this->update(array($column => $value)); }
protected function adjust($column, $amount, $operator) { $wrapped = $this->grammar->wrap($column); $value = Database::raw($wrapped . $operator . $amount); return $this->update(array($column => $value)); }