|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Jenssegers\Mongodb\Helpers; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 7 | +use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
| 8 | +use Jenssegers\Mongodb\Eloquent\Model; |
| 9 | + |
| 10 | +trait QueriesRelationships |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Add a relationship count / exists condition to the query. |
| 14 | + * |
| 15 | + * @param string $relation |
| 16 | + * @param string $operator |
| 17 | + * @param int $count |
| 18 | + * @param string $boolean |
| 19 | + * @param \Closure|null $callback |
| 20 | + * @return \Illuminate\Database\Eloquent\Builder|static |
| 21 | + */ |
| 22 | + public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) |
| 23 | + { |
| 24 | + if (strpos($relation, '.') !== false) { |
| 25 | + return $this->hasNested($relation, $operator, $count, $boolean, $callback); |
| 26 | + } |
| 27 | + |
| 28 | + $relation = $this->getRelationWithoutConstraints($relation); |
| 29 | + |
| 30 | + // If this is a hybrid relation then we can not use a normal whereExists() query that relies on a subquery |
| 31 | + // We need to use a `whereIn` query |
| 32 | + if ($this->getModel() instanceof Model || $this->isAcrossConnections($relation)) { |
| 33 | + return $this->addHybridHas($relation, $operator, $count, $boolean, $callback); |
| 34 | + } |
| 35 | + |
| 36 | + // If we only need to check for the existence of the relation, then we can optimize |
| 37 | + // the subquery to only run a "where exists" clause instead of this full "count" |
| 38 | + // clause. This will make these queries run much faster compared with a count. |
| 39 | + $method = $this->canUseExistsForExistenceCheck($operator, $count) |
| 40 | + ? 'getRelationExistenceQuery' |
| 41 | + : 'getRelationExistenceCountQuery'; |
| 42 | + |
| 43 | + $hasQuery = $relation->{$method}( |
| 44 | + $relation->getRelated()->newQuery(), $this |
| 45 | + ); |
| 46 | + |
| 47 | + // Next we will call any given callback as an "anonymous" scope so they can get the |
| 48 | + // proper logical grouping of the where clauses if needed by this Eloquent query |
| 49 | + // builder. Then, we will be ready to finalize and return this query instance. |
| 50 | + if ($callback) { |
| 51 | + $hasQuery->callScope($callback); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->addHasWhere( |
| 55 | + $hasQuery, $relation, $operator, $count, $boolean |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param $relation |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + protected function isAcrossConnections($relation) |
| 64 | + { |
| 65 | + return $relation->getParent()->getConnectionName() !== $relation->getRelated()->getConnectionName(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Compare across databases |
| 70 | + * @param $relation |
| 71 | + * @param string $operator |
| 72 | + * @param int $count |
| 73 | + * @param string $boolean |
| 74 | + * @param Closure|null $callback |
| 75 | + * @return mixed |
| 76 | + * @throws \Exception |
| 77 | + */ |
| 78 | + public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) |
| 79 | + { |
| 80 | + $hasQuery = $relation->getQuery(); |
| 81 | + if ($callback) { |
| 82 | + $hasQuery->callScope($callback); |
| 83 | + } |
| 84 | + |
| 85 | + // If the operator is <, <= or !=, we will use whereNotIn. |
| 86 | + $not = in_array($operator, ['<', '<=', '!=']); |
| 87 | + // If we are comparing to 0, we need an additional $not flip. |
| 88 | + if ($count == 0) { |
| 89 | + $not = ! $not; |
| 90 | + } |
| 91 | + |
| 92 | + $relations = $hasQuery->pluck($this->getHasCompareKey($relation)); |
| 93 | + |
| 94 | + $relatedIds = $this->getConstrainedRelatedIds($relations, $operator, $count); |
| 95 | + |
| 96 | + return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns key we are constraining this parent model's query with |
| 101 | + * @param $relation |
| 102 | + * @return string |
| 103 | + * @throws \Exception |
| 104 | + */ |
| 105 | + protected function getRelatedConstraintKey($relation) |
| 106 | + { |
| 107 | + if ($relation instanceof HasOneOrMany) { |
| 108 | + return $this->model->getKeyName(); |
| 109 | + } |
| 110 | + |
| 111 | + if ($relation instanceof BelongsTo) { |
| 112 | + return $relation->getForeignKey(); |
| 113 | + } |
| 114 | + |
| 115 | + throw new \Exception(class_basename($relation).' Is Not supported for hybrid query constraints!'); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param $relation |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + protected function getHasCompareKey($relation) |
| 123 | + { |
| 124 | + if (method_exists($relation, 'getHasCompareKey')) { |
| 125 | + return $relation->getHasCompareKey(); |
| 126 | + } |
| 127 | + |
| 128 | + return $relation instanceof HasOneOrMany ? $relation->getForeignKeyName() : $relation->getOwnerKey(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param $relations |
| 133 | + * @param $operator |
| 134 | + * @param $count |
| 135 | + * @return array |
| 136 | + */ |
| 137 | + protected function getConstrainedRelatedIds($relations, $operator, $count) |
| 138 | + { |
| 139 | + $relationCount = array_count_values(array_map(function ($id) { |
| 140 | + return (string) $id; // Convert Back ObjectIds to Strings |
| 141 | + }, is_array($relations) ? $relations : $relations->flatten()->toArray())); |
| 142 | + // Remove unwanted related objects based on the operator and count. |
| 143 | + $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { |
| 144 | + // If we are comparing to 0, we always need all results. |
| 145 | + if ($count == 0) { |
| 146 | + return true; |
| 147 | + } |
| 148 | + switch ($operator) { |
| 149 | + case '>=': |
| 150 | + case '<': |
| 151 | + return $counted >= $count; |
| 152 | + case '>': |
| 153 | + case '<=': |
| 154 | + return $counted > $count; |
| 155 | + case '=': |
| 156 | + case '!=': |
| 157 | + return $counted == $count; |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + // All related ids. |
| 162 | + return array_keys($relationCount); |
| 163 | + } |
| 164 | +} |
0 commit comments