Skip to content

V5 #3041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed

V5 #3041

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

# [4.7.0] - upcoming

* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)

## [4.6.0] - 2024-07-09

* Add `DocumentModel` trait to use any 3rd party model with MongoDB @GromNaN in [#2580](https://github.com/mongodb/laravel-mongodb/pull/2580)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testAggregationBuilderSortStage(): void
// end aggregation sort stage

$this->assertEquals(6, $result->count());
$this->assertEquals('Janet Doe', $result->first()['name']);
$this->assertEquals('Janet Doe', $result->first()->name);
}

public function testAggregationBuilderProjectStage(): void
Expand All @@ -80,8 +80,9 @@ public function testAggregationBuilderProjectStage(): void
// end aggregation project stage

$this->assertEquals(6, $result->count());
$this->assertNotNull($result->first()['name']);
$this->assertArrayNotHasKey('_id', $result->first());
$this->assertNotNull($result->first()->name);
$this->assertObjectNotHasProperty('_id', $result->first());
$this->assertObjectNotHasProperty('id', $result->first());
}

public function testAggregationBuilderPipeline(): void
Expand All @@ -104,7 +105,7 @@ public function testAggregationBuilderPipeline(): void
$result = $pipeline->get();

$this->assertEquals(2, $result->count());
$this->assertNotNull($result->first()['birth_year_avg']);
$this->assertNotNull($result->first()->birth_year_avg);
}

// phpcs:disable Squiz.Commenting.FunctionComment.WrongStyle
Expand All @@ -130,6 +131,6 @@ public function testCustomOperatorFactory(): void
$result = $pipeline->get();

$this->assertEquals(6, $result->count());
$this->assertNotNull($result->first()['birth_year']);
$this->assertNotNull($result->first()->birth_year);
}
}
4 changes: 2 additions & 2 deletions docs/includes/usage-examples/FindOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public function testFindOne(): void

// begin-find-one
$movie = Movie::where('directors', 'Rob Reiner')
->orderBy('_id')
->orderBy('id')
->first();

echo $movie->toJson();
// end-find-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
$this->expectOutputRegex('/^{"title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\],"id":"[a-z0-9]{24}"}$/');
}
}
2 changes: 1 addition & 1 deletion docs/includes/usage-examples/InsertOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function testInsertOne(): void
// end-insert-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","id":"[a-z0-9]{24}"}$/');
}
}
1 change: 0 additions & 1 deletion src/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ class User extends BaseUser
{
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
}
2 changes: 1 addition & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function raw($value = null)
}

// The result is a single object.
if (is_array($results) && array_key_exists('_id', $results)) {
if (is_array($results) && array_key_exists('id', $results)) {
return $this->model->newFromBuilder((array) $results);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function getIdAttribute($value = null)
{
// If we don't have a value for 'id', we will use the MongoDB '_id' value.
// This allows us to work with models in a more sql-like way.
if (! $value && array_key_exists('_id', $this->attributes)) {
$value = $this->attributes['_id'];
if (! $value && array_key_exists('id', $this->attributes)) {
$value = $this->attributes['id'];
}

// Convert ObjectID to string.
Expand Down Expand Up @@ -238,8 +238,8 @@ public function setAttribute($key, $value)
};
}

// Convert _id to ObjectID.
if ($key === '_id' && is_string($value)) {
// Convert "id" to ObjectID.
if ($key === 'id' && is_string($value)) {
$builder = $this->newBaseQueryBuilder();

$value = $builder->convertKey($value);
Expand Down Expand Up @@ -721,9 +721,9 @@ protected function isBSON(mixed $value): bool
public function save(array $options = [])
{
// SQL databases would use autoincrement the id field if set to null.
// Apply the same behavior to MongoDB with _id only, otherwise null would be stored.
if (array_key_exists('_id', $this->attributes) && $this->attributes['_id'] === null) {
unset($this->attributes['_id']);
// Apply the same behavior to MongoDB with "id" only, otherwise null would be stored.
if (array_key_exists('id', $this->attributes) && $this->attributes['id'] === null) {
unset($this->attributes['id']);
}

$saved = parent::save($options);
Expand Down
7 changes: 0 additions & 7 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ abstract class Model extends BaseModel
{
use DocumentModel;

/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = '_id';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary key can keep the default value id, which is converted to _id by the query builder.


/**
* The primary key type.
*
Expand Down
10 changes: 3 additions & 7 deletions src/MongoDBQueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

namespace MongoDB\Laravel;

use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
use Illuminate\Queue\Failed\NullFailedJobProvider;
use Illuminate\Queue\QueueServiceProvider;
use MongoDB\Laravel\Queue\Failed\MongoFailedJobProvider;

use function array_key_exists;
use function trigger_error;

use const E_USER_DEPRECATED;

class MongoDBQueueServiceProvider extends QueueServiceProvider
{
Expand Down Expand Up @@ -52,14 +49,13 @@ protected function registerFailedJobServices()
/**
* Create a new MongoDB failed job provider.
*/
protected function mongoFailedJobProvider(array $config): MongoFailedJobProvider
protected function mongoFailedJobProvider(array $config): DatabaseFailedJobProvider
{
if (! isset($config['collection']) && isset($config['table'])) {
trigger_error('Since mongodb/laravel-mongodb 4.4: Using "table" option for the queue is deprecated. Use "collection" instead.', E_USER_DEPRECATED);
$config['collection'] = $config['table'];
}

return new MongoFailedJobProvider(
return new DatabaseFailedJobProvider(
$this->app['db'],
$config['database'] ?? null,
$config['collection'] ?? 'failed_jobs',
Expand Down
2 changes: 1 addition & 1 deletion src/Query/AggregationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function execute(array $options): CursorInterface&Iterator
$pipeline = $encoder->encode($this->getPipeline());

$options = array_replace(
['typeMap' => ['root' => 'array', 'document' => 'array']],
['typeMap' => ['root' => 'object', 'document' => 'object']],
$this->options,
$options,
);
Expand Down
Loading