diff --git a/.travis.yml b/.travis.yml index ab5e8ce48..27000a962 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.6 - 7 - 7.1 diff --git a/composer.json b/composer.json index c64c72e03..3367145de 100644 --- a/composer.json +++ b/composer.json @@ -11,17 +11,18 @@ ], "license" : "MIT", "require": { - "illuminate/support": "^5.1", - "illuminate/container": "^5.1", - "illuminate/database": "^5.1", - "illuminate/events": "^5.1", + "illuminate/support": "^5.5", + "illuminate/container": "^5.5", + "illuminate/database": "^5.5", + "illuminate/events": "^5.5", "mongodb/mongodb": "^1.0.0" }, "require-dev": { - "phpunit/phpunit": "^5.0|^6.0", + "phpunit/phpunit": "^6.0", "orchestra/testbench": "^3.1", "mockery/mockery": "^0.9", - "satooshi/php-coveralls": "^1.0" + "satooshi/php-coveralls": "^1.0", + "doctrine/dbal": "^2.5" }, "autoload": { "psr-0": { diff --git a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php index 21ba4e1f6..ac7e21862 100644 --- a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php +++ b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php @@ -27,7 +27,7 @@ protected function tokenExpired($token) $date = $token['created_at']->toDateTime(); $date->setTimezone(new DateTimeZone(date_default_timezone_get())); $token['created_at'] = $date->format('Y-m-d H:i:s'); - } elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) { + } elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) { $date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC')); $date->setTimezone(new DateTimeZone(date_default_timezone_get())); $token['created_at'] = $date->format('Y-m-d H:i:s'); diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 233b1bad8..0ada56a86 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb; use Illuminate\Database\Connection as BaseConnection; +use Illuminate\Support\Arr; use MongoDB\Client; class Connection extends BaseConnection @@ -34,7 +35,7 @@ public function __construct(array $config) $dsn = $this->getDsn($config); // You can pass options directly to the MongoDB constructor - $options = array_get($config, 'options', []); + $options = Arr::get($config, 'options', []); // Create the connection $this->connection = $this->createConnection($dsn, $config, $options); diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 3e0f453da..46d715fe9 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -162,10 +162,18 @@ public function raw($expression = null) return $this->model->newFromBuilder((array) $results); } // The result is a single object. - elseif (is_array($results) and array_key_exists('_id', $results)) { + elseif (is_array($results) && array_key_exists('_id', $results)) { return $this->model->newFromBuilder((array) $results); } return $results; } + + /** + * @return \Illuminate\Database\ConnectionInterface + */ + public function getConnection() + { + return $this->query->getConnection(); + } } diff --git a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php index 8150a8b15..307f5e330 100644 --- a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php @@ -2,6 +2,7 @@ namespace Jenssegers\Mongodb\Eloquent; +use Illuminate\Support\Str; use Jenssegers\Mongodb\Relations\EmbedsMany; use Jenssegers\Mongodb\Relations\EmbedsOne; @@ -32,7 +33,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r } if (is_null($foreignKey)) { - $foreignKey = snake_case(class_basename($this)); + $foreignKey = Str::snake(class_basename($this)); } $query = $this->newQuery(); @@ -67,7 +68,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re } if (is_null($foreignKey)) { - $foreignKey = snake_case(class_basename($this)); + $foreignKey = Str::snake(class_basename($this)); } $query = $this->newQuery(); diff --git a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php index f296c0c89..1f707fd05 100644 --- a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php @@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null) * @param string $collection * @param string $foreignKey * @param string $otherKey + * @param string $parentKey + * @param string $relatedKey * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ - public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null) - { + public function belongsToMany( + $related, + $collection = null, + $foreignKey = null, + $otherKey = null, + $parentKey = null, + $relatedKey = null, + $relation = null + ) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. @@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null, // Check if it is a relation with an original model. if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { - return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation); + return parent::belongsToMany( + $related, + $collection, + $foreignKey, + $otherKey, + $parentKey, + $relatedKey, + $relation + ); } // First, we'll need to determine the foreign key and "other key" for the @@ -252,7 +269,16 @@ public function belongsToMany($related, $collection = null, $foreignKey = null, // appropriate query constraint and entirely manages the hydrations. $query = $instance->newQuery(); - return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation); + return new BelongsToMany( + $query, + $this, + $collection, + $foreignKey, + $otherKey, + $parentKey ?: $this->getKeyName(), + $relatedKey ?: $instance->getKeyName(), + $relation + ); } /** diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index 621648c3f..38b3858f8 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -6,6 +6,7 @@ use DateTime; use Illuminate\Database\Eloquent\Model as BaseModel; use Illuminate\Database\Eloquent\Relations\Relation; +use Illuminate\Support\Arr; use Illuminate\Support\Str; use Jenssegers\Mongodb\Query\Builder as QueryBuilder; use MongoDB\BSON\ObjectID; @@ -46,7 +47,7 @@ public function getIdAttribute($value = null) { // If we don't have a value for 'id', we will use the Mongo '_id' value. // This allows us to work with models in a more sql-like way. - if (!$value and array_key_exists('_id', $this->attributes)) { + if (!$value && array_key_exists('_id', $this->attributes)) { $value = $this->attributes['_id']; } @@ -131,12 +132,12 @@ public function getAttribute($key) } // Dot notation support. - if (str_contains($key, '.') and array_has($this->attributes, $key)) { + if (Str::contains($key, '.') && Arr::has($this->attributes, $key)) { return $this->getAttributeValue($key); } // This checks for embedded relation support. - if (method_exists($this, $key) and !method_exists(self::class, $key)) { + if (method_exists($this, $key) && !method_exists(self::class, $key)) { return $this->getRelationValue($key); } @@ -149,8 +150,8 @@ public function getAttribute($key) protected function getAttributeFromArray($key) { // Support keys in dot notation. - if (str_contains($key, '.')) { - return array_get($this->attributes, $key); + if (Str::contains($key, '.')) { + return Arr::get($this->attributes, $key); } return parent::getAttributeFromArray($key); @@ -162,17 +163,17 @@ protected function getAttributeFromArray($key) public function setAttribute($key, $value) { // Convert _id to ObjectID. - if ($key == '_id' and is_string($value)) { + if ($key == '_id' && is_string($value)) { $builder = $this->newBaseQueryBuilder(); $value = $builder->convertKey($value); } // Support keys in dot notation. - elseif (str_contains($key, '.')) { + elseif (Str::contains($key, '.')) { if (in_array($key, $this->getDates()) && $value) { $value = $this->fromDateTime($value); } - array_set($this->attributes, $key, $value); + Arr::set($this->attributes, $key, $value); return; } @@ -199,8 +200,8 @@ public function attributesToArray() // Convert dot-notation dates. foreach ($this->getDates() as $key) { - if (str_contains($key, '.') and array_has($attributes, $key)) { - array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key))); + if (Str::contains($key, '.') && Arr::has($attributes, $key)) { + Arr::set($attributes, $key, (string) $this->asDateTime(Arr::get($attributes, $key))); } } @@ -218,20 +219,36 @@ public function getCasts() /** * @inheritdoc */ - protected function originalIsNumericallyEquivalent($key) + protected function originalIsEquivalent($key, $current) { - $current = $this->attributes[$key]; - $original = $this->original[$key]; + if (!array_key_exists($key, $this->original)) { + return false; + } + + $original = $this->getOriginal($key); + + if ($current === $original) { + return true; + } + + if (null === $current) { + return false; + } - // Date comparison. - if (in_array($key, $this->getDates())) { + if ($this->isDateAttribute($key)) { $current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current; $original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original; return $current == $original; } - return parent::originalIsNumericallyEquivalent($key); + if ($this->hasCast($key)) { + return $this->castAttribute($key, $current) === + $this->castAttribute($key, $original); + } + + return is_numeric($current) && is_numeric($original) + && strcmp((string) $current, (string) $original) === 0; } /** @@ -242,9 +259,7 @@ protected function originalIsNumericallyEquivalent($key) */ public function drop($columns) { - if (!is_array($columns)) { - $columns = [$columns]; - } + $columns = Arr::wrap($columns); // Unset attributes foreach ($columns as $column) { @@ -263,16 +278,14 @@ public function push() if ($parameters = func_get_args()) { $unique = false; - if (count($parameters) == 3) { + if (count($parameters) === 3) { list($column, $values, $unique) = $parameters; } else { list($column, $values) = $parameters; } // Do batch push by default. - if (!is_array($values)) { - $values = [$values]; - } + $values = Arr::wrap($values); $query = $this->setKeysForSaveQuery($this->newQuery()); @@ -294,9 +307,7 @@ public function push() public function pull($column, $values) { // Do batch pull by default. - if (!is_array($values)) { - $values = [$values]; - } + $values = Arr::wrap($values); $query = $this->setKeysForSaveQuery($this->newQuery()); @@ -318,11 +329,11 @@ protected function pushAttributeValues($column, array $values, $unique = false) foreach ($values as $value) { // Don't add duplicate values when we only want unique values. - if ($unique and in_array($value, $current)) { + if ($unique && (!is_array($current) || in_array($value, $current))) { continue; } - array_push($current, $value); + $current[] = $value; } $this->attributes[$column] = $current; @@ -340,11 +351,13 @@ protected function pullAttributeValues($column, array $values) { $current = $this->getAttributeFromArray($column) ?: []; - foreach ($values as $value) { - $keys = array_keys($current, $value); + if (is_array($current)) { + foreach ($values as $value) { + $keys = array_keys($current, $value); - foreach ($keys as $key) { - unset($current[$key]); + foreach ($keys as $key) { + unset($current[$key]); + } } } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index e0127fcc9..736ce2629 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -8,6 +8,7 @@ use Illuminate\Database\Query\Expression; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Illuminate\Support\Str; use Jenssegers\Mongodb\Connection; use MongoCollection; use MongoDB\BSON\ObjectID; @@ -232,7 +233,7 @@ public function getFresh($columns = []) $wheres = $this->compileWheres(); // Use MongoDB's aggregation framework when using grouping or aggregation functions. - if ($this->groups or $this->aggregate or $this->paginating) { + if ($this->groups || $this->aggregate || $this->paginating) { $group = []; $unwinds = []; @@ -286,7 +287,7 @@ public function getFresh($columns = []) } // The _id field is mandatory when using grouping. - if ($group and empty($group['_id'])) { + if ($group && empty($group['_id'])) { $group['_id'] = null; } @@ -563,7 +564,7 @@ public function insertGetId(array $values, $sequence = null) public function update(array $values, array $options = []) { // Use $set as default operator. - if (!starts_with(key($values), '$')) { + if (!Str::startsWith(key($values), '$')) { $values = ['$set' => $values]; } @@ -705,7 +706,7 @@ public function push($column, $value = null, $unique = false) $operator = $unique ? '$addToSet' : '$push'; // Check if we are pushing multiple values. - $batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1)); + $batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1)); if (is_array($column)) { $query = [$operator => $column]; @@ -728,7 +729,7 @@ public function push($column, $value = null, $unique = false) public function pull($column, $value = null) { // Check if we passed an associative array. - $batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1)); + $batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1)); // If we are pulling multiple values, we need to use $pullAll. $operator = $batch ? '$pullAll' : '$pull'; @@ -804,7 +805,7 @@ protected function performUpdate($query, array $options = []) */ public function convertKey($id) { - if (is_string($id) and strlen($id) === 24 and ctype_xdigit($id)) { + if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) { return new ObjectID($id); } @@ -822,7 +823,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' if (func_num_args() == 3) { $operator = &$params[1]; - if (starts_with($operator, '$')) { + if (Str::startsWith($operator, '$')) { $operator = substr($operator, 1); } } @@ -866,7 +867,7 @@ protected function compileWheres() } // Convert id's. - if (isset($where['column']) and ($where['column'] == '_id' or ends_with($where['column'], '._id'))) { + if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '._id'))) { // Multiple values. if (isset($where['values'])) { foreach ($where['values'] as &$value) { @@ -896,7 +897,7 @@ protected function compileWheres() // The next item in a "chain" of wheres devices the boolean of the // first item. So if we see that there are multiple wheres, we will // use the operator of the next where. - if ($i == 0 and count($wheres) > 1 and $where['boolean'] == 'and') { + if ($i == 0 && count($wheres) > 1 && $where['boolean'] == 'and') { $where['boolean'] = $wheres[$i + 1]['boolean']; } @@ -938,10 +939,10 @@ protected function compileWhereBasic(array $where) $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); // Convert like to regular expression. - if (!starts_with($value, '%')) { + if (!Str::startsWith($value, '%')) { $regex = '^' . $regex; } - if (!ends_with($value, '%')) { + if (!Str::endsWith($value, '%')) { $regex = $regex . '$'; } @@ -958,12 +959,12 @@ protected function compileWhereBasic(array $where) // For inverse regexp operations, we can just use the $not operator // and pass it a Regex instence. - if (starts_with($operator, 'not')) { + if (Str::startsWith($operator, 'not')) { $operator = 'not'; } } - if (!isset($operator) or $operator == '=') { + if (!isset($operator) || $operator == '=') { $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index c29cf98e8..73816b049 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; +use Illuminate\Support\Arr; class BelongsToMany extends EloquentBelongsToMany { @@ -93,7 +94,7 @@ public function save(Model $model, array $joining = [], $touch = true) /** * @inheritdoc */ - public function create(array $attributes, array $joining = [], $touch = true) + public function create(array $attributes = [], array $joining = [], $touch = true) { $instance = $this->related->newInstance($attributes); @@ -134,6 +135,8 @@ public function sync($ids, $detaching = true) $records = $this->formatSyncList($ids); + $current = Arr::wrap($current); + $detach = array_diff($current, array_keys($records)); // We need to make sure we pass a clean array, so that it is not interpreted @@ -143,7 +146,7 @@ public function sync($ids, $detaching = true) // Next, we will take the differences of the currents and given IDs and detach // all of the entities that exist in the "current" array but are not in the // the array of the IDs given to the method which will complete the sync. - if ($detaching and count($detach) > 0) { + if ($detaching && count($detach) > 0) { $this->detach($detach); $changes['detached'] = (array) array_map(function ($v) { @@ -184,7 +187,7 @@ public function attach($id, array $attributes = [], $touch = true) $id = $model->getKey(); // Attach the new parent id to the related model. - $model->push($this->foreignKey, $this->parent->getKey(), true); + $model->push($this->foreignPivotKey, $this->parent->getKey(), true); } else { if ($id instanceof Collection) { $id = $id->modelKeys(); @@ -195,7 +198,7 @@ public function attach($id, array $attributes = [], $touch = true) $query->whereIn($this->related->getKeyName(), (array) $id); // Attach the new parent id to the related model. - $query->push($this->foreignKey, $this->parent->getKey(), true); + $query->push($this->foreignPivotKey, $this->parent->getKey(), true); } // Attach the new ids to the parent model. @@ -231,7 +234,7 @@ public function detach($ids = [], $touch = true) } // Remove the relation to the parent. - $query->pull($this->foreignKey, $this->parent->getKey()); + $query->pull($this->foreignPivotKey, $this->parent->getKey()); if ($touch) { $this->touchIfTouching(); @@ -245,7 +248,7 @@ public function detach($ids = [], $touch = true) */ protected function buildDictionary(Collection $results) { - $foreign = $this->foreignKey; + $foreign = $this->foreignPivotKey; // First we will build a dictionary of child models keyed by the foreign key // of the relation so that we will easily and quickly match them to their @@ -286,15 +289,23 @@ public function newRelatedQuery() */ public function getForeignKey() { - return $this->foreignKey; + return $this->foreignPivotKey; + } + + /** + * @inheritdoc + */ + public function getQualifiedForeignPivotKeyName() + { + return $this->foreignPivotKey; } /** * @inheritdoc */ - public function getQualifiedForeignKeyName() + public function getQualifiedRelatedPivotKeyName() { - return $this->foreignKey; + return $this->relatedPivotKey; } /** @@ -324,6 +335,6 @@ protected function formatSyncList(array $records) */ public function getRelatedKey() { - return property_exists($this, 'relatedKey') ? $this->relatedKey : $this->otherKey; + return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey; } } diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index 82dba4f01..f566e921c 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; +use Illuminate\Support\Arr; use MongoDB\BSON\ObjectID; class EmbedsMany extends EmbedsOneOrMany @@ -39,7 +40,7 @@ public function getResults() public function performInsert(Model $model) { // Generate a new key if needed. - if ($model->getKeyName() == '_id' and !$model->getKey()) { + if ($model->getKeyName() == '_id' && !$model->getKey()) { $model->setAttribute('_id', new ObjectID); } @@ -79,7 +80,7 @@ public function performUpdate(Model $model) $foreignKey = $this->getForeignKeyValue($model); // Use array dot notation for better update behavior. - $values = array_dot($model->getDirty(), $this->localKey . '.$.'); + $values = Arr::dot($model->getDirty(), $this->localKey . '.$.'); // Update document in database. $result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php index 857cf0840..f7932467a 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use MongoDB\BSON\ObjectID; class EmbedsOne extends EmbedsOneOrMany @@ -36,7 +37,7 @@ public function getResults() public function performInsert(Model $model) { // Generate a new key if needed. - if ($model->getKeyName() == '_id' and !$model->getKey()) { + if ($model->getKeyName() == '_id' && !$model->getKey()) { $model->setAttribute('_id', new ObjectID); } @@ -71,7 +72,7 @@ public function performUpdate(Model $model) } // Use array dot notation for better update behavior. - $values = array_dot($model->getDirty(), $this->localKey . '.'); + $values = Arr::dot($model->getDirty(), $this->localKey . '.'); $result = $this->getBaseQuery()->update($values); diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php index dd370cada..036769279 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php @@ -94,9 +94,11 @@ public function match(array $models, Collection $results, $relation) /** * Shorthand to get the results of the relationship. * + * @param array $columns + * * @return Collection */ - public function get() + public function get($columns = ['*']) { return $this->getResults(); } @@ -280,7 +282,12 @@ protected function toModel($attributes = []) return; } - $model = $this->related->newFromBuilder((array) $attributes); + $connection = $this->related->getConnection(); + + $model = $this->related->newFromBuilder( + (array) $attributes, + $connection ? $connection->getName() : null + ); $model->setParentRelation($this); diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 7939c92e7..6575021f7 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -156,7 +156,7 @@ public function sparse($columns = null, $options = []) */ public function geospatial($columns = null, $index = '2d', $options = []) { - if ($index == '2d' or $index == '2dsphere') { + if ($index == '2d' || $index == '2dsphere') { $columns = $this->fluent($columns); $columns = array_flip($columns); diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index b0507c7ef..aadbbe7c0 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -21,6 +21,7 @@ public function testEmbedsManySave() $address = new Address(['city' => 'London']); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address); @@ -46,6 +47,7 @@ public function testEmbedsManySave() $this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all()); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address); @@ -211,6 +213,7 @@ public function testEmbedsManyDestroy() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); @@ -249,6 +252,7 @@ public function testEmbedsManyDelete() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); @@ -297,6 +301,7 @@ public function testEmbedsManyCreatingEventReturnsFalse() $address = new Address(['city' => 'London']); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false); @@ -311,6 +316,7 @@ public function testEmbedsManySavingEventReturnsFalse() $address->exists = true; $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false); $this->assertFalse($user->addresses()->save($address)); @@ -324,6 +330,7 @@ public function testEmbedsManyUpdatingEventReturnsFalse() $user->addresses()->save($address); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false); @@ -341,6 +348,7 @@ public function testEmbedsManyDeletingEventReturnsFalse() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false); $this->assertEquals(0, $user->addresses()->destroy($address)); @@ -444,6 +452,7 @@ public function testEmbedsOne() $father = new User(['name' => 'Mark Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father); @@ -463,6 +472,7 @@ public function testEmbedsOne() $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($father), $father); @@ -478,6 +488,7 @@ public function testEmbedsOne() $father = new User(['name' => 'Jim Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father); @@ -496,6 +507,7 @@ public function testEmbedsOneAssociate() $father = new User(['name' => 'Mark Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything()); $events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father); $father = $user->father()->associate($father); diff --git a/tests/models/Address.php b/tests/models/Address.php index 43adff20a..f2f1278e1 100644 --- a/tests/models/Address.php +++ b/tests/models/Address.php @@ -4,6 +4,7 @@ class Address extends Eloquent { + protected $connection = 'mongodb'; protected static $unguarded = true; public function addresses() diff --git a/tests/models/Book.php b/tests/models/Book.php index e893d2711..1cf8d22cb 100644 --- a/tests/models/Book.php +++ b/tests/models/Book.php @@ -4,6 +4,7 @@ class Book extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'books'; protected static $unguarded = true; protected $primaryKey = 'title'; diff --git a/tests/models/Client.php b/tests/models/Client.php index 5289c1cba..b8309deef 100644 --- a/tests/models/Client.php +++ b/tests/models/Client.php @@ -4,6 +4,7 @@ class Client extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'clients'; protected static $unguarded = true; diff --git a/tests/models/Group.php b/tests/models/Group.php index 831fda0fd..494836ad9 100644 --- a/tests/models/Group.php +++ b/tests/models/Group.php @@ -4,11 +4,12 @@ class Group extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'groups'; protected static $unguarded = true; public function users() { - return $this->belongsToMany('User', null, 'groups', 'users'); + return $this->belongsToMany('User', 'users', 'groups', 'users', '_id', '_id', 'users'); } } diff --git a/tests/models/Item.php b/tests/models/Item.php index babb7328d..ac52226db 100644 --- a/tests/models/Item.php +++ b/tests/models/Item.php @@ -4,6 +4,7 @@ class Item extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'items'; protected static $unguarded = true; diff --git a/tests/models/Location.php b/tests/models/Location.php index c491dbe7a..aa5f36a57 100644 --- a/tests/models/Location.php +++ b/tests/models/Location.php @@ -4,6 +4,7 @@ class Location extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'locations'; protected static $unguarded = true; } diff --git a/tests/models/Photo.php b/tests/models/Photo.php index 533e09a92..81a5cc2de 100644 --- a/tests/models/Photo.php +++ b/tests/models/Photo.php @@ -4,6 +4,7 @@ class Photo extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'photos'; protected static $unguarded = true; diff --git a/tests/models/Role.php b/tests/models/Role.php index 2d318df9a..a59ce7e02 100644 --- a/tests/models/Role.php +++ b/tests/models/Role.php @@ -4,6 +4,7 @@ class Role extends Eloquent { + protected $connection = 'mongodb'; protected $collection = 'roles'; protected static $unguarded = true; diff --git a/tests/models/Soft.php b/tests/models/Soft.php index 587ba66cc..783cf0289 100644 --- a/tests/models/Soft.php +++ b/tests/models/Soft.php @@ -7,6 +7,7 @@ class Soft extends Eloquent { use SoftDeletes; + protected $connection = 'mongodb'; protected $collection = 'soft'; protected static $unguarded = true; protected $dates = ['deleted_at']; diff --git a/tests/models/User.php b/tests/models/User.php index 3834b6e0f..2d34dd8b1 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -10,6 +10,7 @@ class User extends Eloquent implements AuthenticatableContract, CanResetPassword { use Authenticatable, CanResetPassword; + protected $connection = 'mongodb'; protected $dates = ['birthday', 'entry.date']; protected static $unguarded = true; @@ -45,7 +46,7 @@ public function clients() public function groups() { - return $this->belongsToMany('Group', null, 'users', 'groups'); + return $this->belongsToMany('Group', 'groups', 'users', 'groups', '_id', '_id', 'groups'); } public function photos()