Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 046b92a

Browse files
GromNaNrustagir
andauthored
PHPORM-220 Deprecate using the $collection property to customize the name (#3064)
Co-authored-by: rustagir <rea.rustagi@mongodb.com>
1 parent d52f24c commit 046b92a

File tree

24 files changed

+34
-24
lines changed

24 files changed

+34
-24
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
77
* Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062)
8+
* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064)
89

910
## [4.7.0] - 2024年07月19日
1011

‎docs/eloquent-models/model-class.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Change the Model Collection Name
111111

112112
By default, the model uses the snake case plural form of your model
113113
class name. To change the name of the collection the model uses to retrieve
114-
and save data in MongoDB, override the ``$collection`` property of the model
114+
and save data in MongoDB, override the ``$table`` property of the model
115115
class.
116116

117117
.. note::
@@ -127,7 +127,7 @@ The following example specifies the custom MongoDB collection name,
127127
:emphasize-lines: 9
128128
:dedent:
129129

130-
Without overriding the ``$collection`` property, this model maps to the
130+
Without overriding the ``$table`` property, this model maps to the
131131
``planets`` collection. With the overridden property, the example class stores
132132
the model in the ``celestial_body`` collection.
133133

‎docs/includes/auth/AuthUser.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class User extends Authenticatable
88
{
99
protected $connection = 'mongodb';
10-
protected $collection = 'users';
10+
protected $table = 'users';
1111

1212
protected $fillable = [
1313
'name',

‎docs/includes/auth/PersonalAccessToken.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PersonalAccessToken extends SanctumToken
1010
use DocumentModel;
1111

1212
protected $connection = 'mongodb';
13-
protected $collection = 'personal_access_tokens';
13+
protected $table = 'personal_access_tokens';
1414
protected $primaryKey = '_id';
1515
protected $keyType = 'string';
1616
}

‎docs/includes/eloquent-models/PlanetCollection.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
class Planet extends Model
88
{
9-
protected $collection = 'celestial_body';
9+
protected $table = 'celestial_body';
1010
}

‎docs/includes/fundamentals/read-operations/Movie.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
class Movie extends Model
88
{
99
protected $connection = 'mongodb';
10-
protected $collection = 'movies';
10+
protected $table = 'movies';
1111
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
1212
}

‎docs/includes/usage-examples/Movie.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
class Movie extends Model
88
{
99
protected $connection = 'mongodb';
10-
protected $collection = 'movies';
10+
protected $table = 'movies';
1111
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
1212
}

‎src/Eloquent/DocumentModel.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@
4646
use function str_contains;
4747
use function str_starts_with;
4848
use function strcmp;
49+
use function trigger_error;
4950
use function var_export;
5051

52+
use const E_USER_DEPRECATED;
53+
5154
trait DocumentModel
5255
{
5356
use HybridRelations;
@@ -141,7 +144,13 @@ public function freshTimestamp()
141144
/** @inheritdoc */
142145
public function getTable()
143146
{
144-
return $this->collection ?? parent::getTable();
147+
if (isset($this->collection)) {
148+
trigger_error('Since mongodb/laravel-mongodb 4.8: Using "$collection" property is deprecated. Use "$table" instead.', E_USER_DEPRECATED);
149+
150+
return $this->collection;
151+
}
152+
153+
return parent::getTable();
145154
}
146155

147156
/** @inheritdoc */

‎tests/Models/Birthday.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Birthday extends Model
1919
protected $primaryKey = '_id';
2020
protected $keyType = 'string';
2121
protected $connection = 'mongodb';
22-
protected string$collection = 'birthday';
22+
protected $table = 'birthday';
2323
protected $fillable = ['name', 'birthday'];
2424

2525
protected $casts = ['birthday' => 'datetime'];

‎tests/Models/Book.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Book extends Model
2020
protected $primaryKey = 'title';
2121
protected $keyType = 'string';
2222
protected $connection = 'mongodb';
23-
protected string$collection = 'books';
23+
protected $table = 'books';
2424
protected static $unguarded = true;
2525

2626
public function author(): BelongsTo

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /