Uploaded image for project: 'PHP ORMs'
  1. PHP ORMs
  2. PHPORM-203

laravel-mongodb - Issue #2120: Update model to implement a trait to set mongo functionality

    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      For any third party module that defines a Model class, it is possible to make this model class compatible with MongoDB by extending the class and using the new DocumentModel trait.

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      Examples in the PR description: https://github.com/mongodb/laravel-mongodb/pull/2580

      3. Which versions of the driver/connector does this apply to?

      Since Laravel-MongoDB 4.6.0

      Show
      1. What would you like to communicate to the user about this feature? For any third party module that defines a Model class, it is possible to make this model class compatible with MongoDB by extending the class and using the new DocumentModel trait. 2. Would you like the user to see examples of the syntax and/or executable code and its output? Examples in the PR description: https://github.com/mongodb/laravel-mongodb/pull/2580 3. Which versions of the driver/connector does this apply to? Since Laravel-MongoDB 4.6.0

      michaeljennings has created Issue #2120: Update model to implement a trait to set mongo functionality in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:
      In a project I'm working on we use laravel passport for our authentication. Up until now we've been using the [designmynight/laravel-mongodb-passport](https://github.com/designmynight/laravel-mongodb-passport) package to update the passport models to work with mongo. This package adds copies of each of the passport models but extends the mongodb base model instead.

      We've just updated to version 10 of passport and you can no longer do this as a lot of the classes now type hint the default passport models, this means you now have to extend the passport models.

      For example you could override the default token model easily by doing `Passport::useTokenModel(CustomTokenModel::class)`, but in the `Laravel\Passport\TokenRepository` class it type hints the default token model in save method.

      ```php
      /**

      • Store the given token instance.
        *
      • @param \Laravel\Passport\Token $token
      • @return void
        */
        public function save(Token $token) { $token->save(); }

        ```

      This leaves us in a situation where we either have to go through and make custom versions of every passport class that has a type hint in, or to extend every passport model and copy the contents of the mongodb model into it.

      I'm proposing to change the mongo db model so it implements a trait to set the mongo specific functionality.

      The model would then end up looking something like this:

      ```php
      namespace Jenssegers\Mongodb\Eloquent;

      use Illuminate\Database\Eloquent\Model as BaseModel;

      abstract class Model extends BaseModel

      { use Moloquent; /** * The primary key for the model. * @var string */ protected $primaryKey = '_id'; /** * The primary key type. * @var string */ protected $keyType = 'string'; }

      ```

      The `Moloquent` trait would then have all of the current methods and mongo specific properties from the current model.

      This would allow us to extend models from other projects more easily and then just implement the `Moloquent` trait to make it work with mongo. So our token class could then become:

      ```php
      class Token extends \Laravel\Passport\Token

      { use Moloquent; }

      ```

      I've tried cloning the project and implementing the trait instead and all of the tests still pass.

      I'm happy to submit a merge request to show how the trait would work but let me know your thoughts.

            Assignee:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: