Skip to content
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

Open
wants to merge 3 commits into
base: 4.6
Choose a base branch
from
Open

Move model to a trait #2580

wants to merge 3 commits into from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 28, 2023

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.

@GromNaN
Copy link
Member Author

GromNaN commented Aug 29, 2023

Currently, the Model class is a marker for MongoDB models. If a trait is used, an other marker will be necessary.
https://github.com/mongodb-php/laravel-mongodb/blob/f06f7b3dca88cfd5cc6a823ff01051b4a46ca10a/src/Eloquent/HybridRelations.php#L145

src/Eloquent/Model.php Fixed Show fixed Hide fixed
@GromNaN GromNaN changed the base branch from 4.1 to 4.5 July 4, 2024 18:52
@GromNaN GromNaN force-pushed the model-trait branch 3 times, most recently from 5d0d693 to 2163745 Compare July 5, 2024 12:36
@GromNaN GromNaN marked this pull request as ready for review July 5, 2024 12:39
@GromNaN GromNaN requested a review from a team as a code owner July 5, 2024 12:39
@GromNaN GromNaN requested a review from alcaeus July 5, 2024 12:39
Copy link
Member Author

@GromNaN GromNaN left a 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';
Copy link
Member Author

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();
Copy link
Member Author

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;
Copy link
Member Author

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
Copy link
Member Author

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;
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update model to implement a trait to set mongo functionality
1 participant