-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Move model to a trait #2580
Move model to a trait #2580
Conversation
Currently, the |
5d0d693
to
2163745
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trait DocumentModel
is extracted from the MongoDB\Laravel\Eloquent\Model
class, the only changes are commented.
@@ -76,699 +28,10 @@ abstract class Model extends BaseModel | |||
*/ | |||
protected $keyType = 'string'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the trait is used, the properties $keyType
and $primaryKey
must be defined, because they can't be redefined by the trait.
/** @inheritdoc */ | ||
public function getTable() | ||
{ | ||
return $this->collection ?? parent::getTable(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This property is not defined in the trait, otherwise it's allowed to redefine in the class that uses the trait.
/** | ||
* The parent relation instance. | ||
*/ | ||
private Relation $parentRelation; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the visibility to private
. There is a getter and an setter for this method. It doesn't need to be protected. Since it's a trait, it can still be accessed directly in a class that uses the trait.
src/Eloquent/Model.php
Outdated
*/ | ||
public function unset($columns) | ||
public static function isDocumentModel(string|object $related): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method replaces $class instanceof MongoDB\Laravel\Eloquent\Model
to check if the model class is for a MongoDB connection. It is public and can be used in projects.
{ | ||
use DocumentModel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the test models have been updated to use the trait, so that we are sure all the features works without extending MongoDB\Laravel\Eloquent\Model
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having one test with a model that extends our document model class instead of using the trait will help us prevent regressions in our model class.
src/Eloquent/Model.php
Outdated
|
||
return $this; | ||
return is_subclass_of($related, BaseModel::class) | ||
&& array_key_exists(DocumentModel::class, class_uses_recursive($related)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class_uses_recursive
looks like it has significant performance implications. Using a marker interface and checking using is_subclass_of
would probably be more efficient.
Alternatively, checking whether the class is a subclass of this model class first, and only checking for traits in other cases would probably solve the performance issue for most cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a shortcut to check our Model class + a memory cache for when the same class is checked twice in the same request.
|
||
class Casting extends Eloquent | ||
class Casting extends Model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class continues to extend MongoDB\Laravel\Eloquent\Model
@GromNaN Hopefully in documentation can add a guide for third party packages like spatie's laravel-permissions, bouncer, tenancy etc, sanctum, etc. Or create a community for laravel packages being modified to work with laravel mongodb. This way all popular packages will have counterpart that is made to work with mongodb thanks. |
@masterbater The documentation is going to be written by MongoDB's doc team. |
Fix #2120
Fix #2445
Fix #2991
By moving all the logic to a trait, it makes it possible to reuse entities provided by community projects. Example: https://github.com/z-song/laravel-admin/blob/master/src/Auth/Database/Administrator.php
The properties
$primaryKey
and$keyType
can't be in the trait because PHP forbid declaring a property in a trait that is different (type and value) than the property declared in the parent class.For Laravel's
User
model:For Laravel Sanctum (with default model override):
I have mixed feelings about this change, as it breaks the link with the overloaded parent methods. It would probably be better to make the change in each community-package when necessary.