Skip to content

PHPORM-220 Deprecate using the $collection property to customize the name #3064

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

Merged
merged 3 commits into from
Jul 23, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/eloquent-models/model-class.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion docs/includes/auth/AuthUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class User extends Authenticatable
{
protected $connection = 'mongodb';
protected $collection = 'users';
protected $table = 'users';

protected $fillable = [
'name',
Expand Down
2 changes: 1 addition & 1 deletion docs/includes/auth/PersonalAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
2 changes: 1 addition & 1 deletion docs/includes/eloquent-models/PlanetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class Planet extends Model
{
protected $collection = 'celestial_body';
protected $table = 'celestial_body';
}
2 changes: 1 addition & 1 deletion docs/includes/fundamentals/read-operations/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
class Movie extends Model
{
protected $connection = 'mongodb';
protected $collection = 'movies';
protected $table = 'movies';
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
}
2 changes: 1 addition & 1 deletion docs/includes/usage-examples/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
class Movie extends Model
{
protected $connection = 'mongodb';
protected $collection = 'movies';
protected $table = 'movies';
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
}
11 changes: 10 additions & 1 deletion src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Birthday.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Casting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Casting extends Model
{
protected $connection = 'mongodb';
protected string $collection = 'casting';
protected $table = 'casting';

protected $fillable = [
'uuid',
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Experience.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Guarded.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
2 changes: 1 addition & 1 deletion tests/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion tests/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/SchemaVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Scoped.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Skill.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Soft.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
Loading