Viewing file: Builder.php (7.01 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php namespace Jenssegers\Mongodb\Eloquent;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Relations\Relation; use MongoCursor;
class Builder extends EloquentBuilder { /** * The methods that should be returned from query builder. * * @var array */ protected $passthru = [ 'toSql', 'lists', 'insert', 'insertGetId', 'pluck', 'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', ];
/** * Update a record in the database. * * @param array $values * @param array $options * @return int */ public function update(array $values, array $options = []) { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $relation->performUpdate($this->model, $values);
return 1; }
return $this->query->update($this->addUpdatedAtColumn($values), $options); }
/** * Insert a new record into the database. * * @param array $values * @return bool */ public function insert(array $values) { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $relation->performInsert($this->model, $values);
return true; }
return parent::insert($values); }
/** * Insert a new record and get the value of the primary key. * * @param array $values * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $relation->performInsert($this->model, $values);
return $this->model->getKey(); }
return parent::insertGetId($values, $sequence); }
/** * Delete a record from the database. * * @return mixed */ public function delete() { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $relation->performDelete($this->model);
return $this->model->getKey(); }
return parent::delete(); }
/** * Increment a column's value by a given amount. * * @param string $column * @param int $amount * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = []) { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $value = $this->model->{$column};
// When doing increment and decrements, Eloquent will automatically // sync the original attributes. We need to change the attribute // temporary in order to trigger an update query. $this->model->{$column} = null;
$this->model->syncOriginalAttribute($column);
$result = $this->model->update([$column => $value]);
return $result; }
return parent::increment($column, $amount, $extra); }
/** * Decrement a column's value by a given amount. * * @param string $column * @param int $amount * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = []) { // Intercept operations on embedded models and delegate logic // to the parent relation instance. if ($relation = $this->model->getParentRelation()) { $value = $this->model->{$column};
// When doing increment and decrements, Eloquent will automatically // sync the original attributes. We need to change the attribute // temporary in order to trigger an update query. $this->model->{$column} = null;
$this->model->syncOriginalAttribute($column);
return $this->model->update([$column => $value]); }
return parent::decrement($column, $amount, $extra); }
/** * Add the "has" condition where clause to the query. * * @param \Illuminate\Database\Eloquent\Builder $hasQuery * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $operator * @param int $count * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder */ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean) { $query = $hasQuery->getQuery();
// Get the number of related objects for each possible parent. $relationCount = array_count_values($query->lists($relation->getHasCompareKey()));
// Remove unwanted related objects based on the operator and count. $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { // If we are comparing to 0, we always need all results. if ($count == 0) { return true; }
switch ($operator) { case '>=': case '<': return $counted >= $count; case '>': case '<=': return $counted > $count; case '=': case '!=': return $counted == $count; } });
// If the operator is <, <= or !=, we will use whereNotIn. $not = in_array($operator, ['<', '<=', '!=']);
// If we are comparing to 0, we need an additional $not flip. if ($count == 0) { $not = !$not; }
// All related ids. $relatedIds = array_keys($relationCount);
// Add whereIn to the query. return $this->whereIn($this->model->getKeyName(), $relatedIds, $boolean, $not); }
/** * Create a raw database expression. * * @param closure $expression * @return mixed */ public function raw($expression = null) { // Get raw results from the query builder. $results = $this->query->raw($expression);
// Convert MongoCursor results to a collection of models. if ($results instanceof MongoCursor) { $results = iterator_to_array($results, false);
return $this->model->hydrate($results); }
// The result is a single object. elseif (is_array($results) and array_key_exists('_id', $results)) { $model = $this->model->newFromBuilder($results);
$model->setConnection($this->model->getConnection());
return $model; }
return $results; } }
|