From 3dca19c6229577bca6bd1b0e55a772ffcf5f48b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 22 Jul 2024 17:37:34 +0200 Subject: [PATCH 1/3] PHPORM-220 Deprecate using the $collection property to customize the collection name --- docs/includes/auth/AuthUser.php | 2 +- docs/includes/auth/PersonalAccessToken.php | 2 +- docs/includes/eloquent-models/PlanetCollection.php | 2 +- docs/includes/fundamentals/read-operations/Movie.php | 2 +- docs/includes/usage-examples/Movie.php | 2 +- src/Eloquent/DocumentModel.php | 11 ++++++++++- tests/Models/Birthday.php | 2 +- tests/Models/Book.php | 2 +- tests/Models/Casting.php | 2 +- tests/Models/Client.php | 2 +- tests/Models/Experience.php | 2 +- tests/Models/Group.php | 2 +- tests/Models/Guarded.php | 2 +- tests/Models/Item.php | 2 +- tests/Models/Label.php | 2 +- tests/Models/Location.php | 2 +- tests/Models/Photo.php | 2 +- tests/Models/Role.php | 2 +- tests/Models/SchemaVersion.php | 2 +- tests/Models/Scoped.php | 2 +- tests/Models/Skill.php | 2 +- tests/Models/Soft.php | 2 +- 22 files changed, 31 insertions(+), 22 deletions(-) diff --git a/docs/includes/auth/AuthUser.php b/docs/includes/auth/AuthUser.php index 8b6a0f173..439f923c4 100644 --- a/docs/includes/auth/AuthUser.php +++ b/docs/includes/auth/AuthUser.php @@ -7,7 +7,7 @@ class User extends Authenticatable { protected $connection = 'mongodb'; - protected $collection = 'users'; + protected $table = 'users'; protected $fillable = [ 'name', diff --git a/docs/includes/auth/PersonalAccessToken.php b/docs/includes/auth/PersonalAccessToken.php index 2a3c5e29c..165758770 100644 --- a/docs/includes/auth/PersonalAccessToken.php +++ b/docs/includes/auth/PersonalAccessToken.php @@ -10,7 +10,7 @@ class PersonalAccessToken extends SanctumToken use DocumentModel; protected $connection = 'mongodb'; - protected $collection = 'personal_access_tokens'; + protected $table = 'personal_access_tokens'; protected $primaryKey = '_id'; protected $keyType = 'string'; } diff --git a/docs/includes/eloquent-models/PlanetCollection.php b/docs/includes/eloquent-models/PlanetCollection.php index b36b24daa..f2c894db6 100644 --- a/docs/includes/eloquent-models/PlanetCollection.php +++ b/docs/includes/eloquent-models/PlanetCollection.php @@ -6,5 +6,5 @@ class Planet extends Model { - protected $collection = 'celestial_body'; + protected $table = 'celestial_body'; } diff --git a/docs/includes/fundamentals/read-operations/Movie.php b/docs/includes/fundamentals/read-operations/Movie.php index 728a066de..9b73e8738 100644 --- a/docs/includes/fundamentals/read-operations/Movie.php +++ b/docs/includes/fundamentals/read-operations/Movie.php @@ -7,6 +7,6 @@ class Movie extends Model { protected $connection = 'mongodb'; - protected $collection = 'movies'; + protected $table = 'movies'; protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot']; } diff --git a/docs/includes/usage-examples/Movie.php b/docs/includes/usage-examples/Movie.php index 728a066de..9b73e8738 100644 --- a/docs/includes/usage-examples/Movie.php +++ b/docs/includes/usage-examples/Movie.php @@ -7,6 +7,6 @@ class Movie extends Model { protected $connection = 'mongodb'; - protected $collection = 'movies'; + protected $table = 'movies'; protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot']; } diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index 15c33ef16..cbc388b22 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -46,8 +46,11 @@ use function str_contains; use function str_starts_with; use function strcmp; +use function trigger_error; use function var_export; +use const E_USER_DEPRECATED; + trait DocumentModel { use HybridRelations; @@ -141,7 +144,13 @@ public function freshTimestamp() /** @inheritdoc */ public function getTable() { - return $this->collection ?? parent::getTable(); + if (isset($this->collection)) { + trigger_error('Since mongodb/laravel-mongodb 4.8: Using "$collection" property is deprecated. Use "$table" instead.', E_USER_DEPRECATED); + + return $this->collection; + } + + return parent::getTable(); } /** @inheritdoc */ diff --git a/tests/Models/Birthday.php b/tests/Models/Birthday.php index 65b703af1..ae0e108b1 100644 --- a/tests/Models/Birthday.php +++ b/tests/Models/Birthday.php @@ -19,7 +19,7 @@ class Birthday extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'birthday'; + protected $table = 'birthday'; protected $fillable = ['name', 'birthday']; protected $casts = ['birthday' => 'datetime']; diff --git a/tests/Models/Book.php b/tests/Models/Book.php index 5bee76e5c..3293a0eaa 100644 --- a/tests/Models/Book.php +++ b/tests/Models/Book.php @@ -20,7 +20,7 @@ class Book extends Model protected $primaryKey = 'title'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'books'; + protected $table = 'books'; protected static $unguarded = true; public function author(): BelongsTo diff --git a/tests/Models/Casting.php b/tests/Models/Casting.php index d033cf444..dd2fadce1 100644 --- a/tests/Models/Casting.php +++ b/tests/Models/Casting.php @@ -10,7 +10,7 @@ class Casting extends Model { protected $connection = 'mongodb'; - protected string $collection = 'casting'; + protected $table = 'casting'; protected $fillable = [ 'uuid', diff --git a/tests/Models/Client.php b/tests/Models/Client.php index 47fd91d03..b0339a0e5 100644 --- a/tests/Models/Client.php +++ b/tests/Models/Client.php @@ -17,7 +17,7 @@ class Client extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'clients'; + protected $table = 'clients'; protected static $unguarded = true; public function users(): BelongsToMany diff --git a/tests/Models/Experience.php b/tests/Models/Experience.php index 37a44e4d1..6a306afe1 100644 --- a/tests/Models/Experience.php +++ b/tests/Models/Experience.php @@ -15,7 +15,7 @@ class Experience extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'experiences'; + protected $table = 'experiences'; protected static $unguarded = true; protected $casts = ['years' => 'int']; diff --git a/tests/Models/Group.php b/tests/Models/Group.php index 689c6d599..57c3af59c 100644 --- a/tests/Models/Group.php +++ b/tests/Models/Group.php @@ -15,7 +15,7 @@ class Group extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'groups'; + protected $table = 'groups'; protected static $unguarded = true; public function users(): BelongsToMany diff --git a/tests/Models/Guarded.php b/tests/Models/Guarded.php index 9837e9222..40d11bea5 100644 --- a/tests/Models/Guarded.php +++ b/tests/Models/Guarded.php @@ -14,6 +14,6 @@ class Guarded extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'guarded'; + protected $table = 'guarded'; protected $guarded = ['foobar', 'level1->level2']; } diff --git a/tests/Models/Item.php b/tests/Models/Item.php index bc0b29b7b..2beb40d75 100644 --- a/tests/Models/Item.php +++ b/tests/Models/Item.php @@ -18,7 +18,7 @@ class Item extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'items'; + protected $table = 'items'; protected static $unguarded = true; public function user(): BelongsTo diff --git a/tests/Models/Label.php b/tests/Models/Label.php index b392184d7..b95aa0dcf 100644 --- a/tests/Models/Label.php +++ b/tests/Models/Label.php @@ -20,7 +20,7 @@ class Label extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'labels'; + protected $table = 'labels'; protected static $unguarded = true; protected $fillable = [ diff --git a/tests/Models/Location.php b/tests/Models/Location.php index 9621d388f..2c62dbda9 100644 --- a/tests/Models/Location.php +++ b/tests/Models/Location.php @@ -14,6 +14,6 @@ class Location extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'locations'; + protected $table = 'locations'; protected static $unguarded = true; } diff --git a/tests/Models/Photo.php b/tests/Models/Photo.php index ea3321337..be7f3666c 100644 --- a/tests/Models/Photo.php +++ b/tests/Models/Photo.php @@ -15,7 +15,7 @@ class Photo extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'photos'; + protected $table = 'photos'; protected static $unguarded = true; public function hasImage(): MorphTo diff --git a/tests/Models/Role.php b/tests/Models/Role.php index 7d0dce7b1..e9f3fa95d 100644 --- a/tests/Models/Role.php +++ b/tests/Models/Role.php @@ -15,7 +15,7 @@ class Role extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'roles'; + protected $table = 'roles'; protected static $unguarded = true; public function user(): BelongsTo diff --git a/tests/Models/SchemaVersion.php b/tests/Models/SchemaVersion.php index cacfc3f65..8acd73545 100644 --- a/tests/Models/SchemaVersion.php +++ b/tests/Models/SchemaVersion.php @@ -14,7 +14,7 @@ class SchemaVersion extends Eloquent public const SCHEMA_VERSION = 2; protected $connection = 'mongodb'; - protected $collection = 'documentVersion'; + protected $table = 'documentVersion'; protected static $unguarded = true; public function migrateSchema(int $fromVersion): void diff --git a/tests/Models/Scoped.php b/tests/Models/Scoped.php index 84b8b81f7..6850dcb21 100644 --- a/tests/Models/Scoped.php +++ b/tests/Models/Scoped.php @@ -15,7 +15,7 @@ class Scoped extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'scoped'; + protected $table = 'scoped'; protected $fillable = ['name', 'favorite']; protected static function boot() diff --git a/tests/Models/Skill.php b/tests/Models/Skill.php index 90c9455b9..1e2daaf80 100644 --- a/tests/Models/Skill.php +++ b/tests/Models/Skill.php @@ -15,7 +15,7 @@ class Skill extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'skills'; + protected $table = 'skills'; protected static $unguarded = true; public function sqlUsers(): BelongsToMany diff --git a/tests/Models/Soft.php b/tests/Models/Soft.php index 549e63758..cbfa2ef23 100644 --- a/tests/Models/Soft.php +++ b/tests/Models/Soft.php @@ -21,7 +21,7 @@ class Soft extends Model protected $primaryKey = '_id'; protected $keyType = 'string'; protected $connection = 'mongodb'; - protected string $collection = 'soft'; + protected $table = 'soft'; protected static $unguarded = true; protected $casts = ['deleted_at' => 'datetime']; From 7b80b10791db07f1c72c0e572e6bed2ce4fd7318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 23 Jul 2024 16:27:19 +0200 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a945c26..4cffde726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. * Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550) * Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062) +* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064) ## [4.7.0] - 2024-07-19 From 804d14128a9b65ea56dbba4af1e37ad74e741f3a Mon Sep 17 00:00:00 2001 From: rustagir Date: Tue, 23 Jul 2024 10:58:11 -0400 Subject: [PATCH 3/3] changes to text --- docs/eloquent-models/model-class.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/eloquent-models/model-class.txt b/docs/eloquent-models/model-class.txt index ad5565abe..8515c83bd 100644 --- a/docs/eloquent-models/model-class.txt +++ b/docs/eloquent-models/model-class.txt @@ -108,7 +108,7 @@ Change the Model Collection Name By default, the model uses the snake case plural form of your model class name. To change the name of the collection the model uses to retrieve -and save data in MongoDB, override the ``$collection`` property of the model +and save data in MongoDB, override the ``$table`` property of the model class. .. note:: @@ -124,7 +124,7 @@ The following example specifies the custom MongoDB collection name, :emphasize-lines: 9 :dedent: -Without overriding the ``$collection`` property, this model maps to the +Without overriding the ``$table`` property, this model maps to the ``planets`` collection. With the overridden property, the example class stores the model in the ``celestial_body`` collection.