-
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
base: 4.6
Are you sure you want to change the base?
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.
*/ | ||
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
.
Fix #2120
By moving all the logic to a trait, it make 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.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.