Giter Club home page Giter Club logo

Comments (3)

ravanscafi avatar ravanscafi commented on July 21, 2024

@bradleygreen, a lot of things has changed. In the past, we use to have a Document Assembler that would take an array from the database and then build an model. We took other directions to allow direct object unserialization from database. To fix your collections, you now need to have a __pclass key on each document telling which class it belongs to.

Note: try this with a test database, not directly on production or some sensitive data!

// make sure to replace both occurrences of `\My\Namespace\Product::class` with your Product class

app(\My\Namespace\Product::class)->getCollection()->updateMany(
    [],
    [
        '$set' => [
            '__pclass' => new \MongoDB\BSON\Binary(\My\Namespace\Product::class, \MongoDB\BSON\Binary::TYPE_USER_DEFINED)
        ]
    ]
);

now, if you do:

\My\Namespace\Product::first();

it should work.

Basically you will need to do this to all of your collections.

You can leverage migrations to do this on production/etc:

php artisan make:mongolid-migration change_product_pclass

and create the logic, then run

php artisan mongolid-migrate

Refer to this documentation to learn more about __pclass and mongodb serialization/unserialization.

I hope that it helps!

from mongolid-laravel.

bradleygreen avatar bradleygreen commented on July 21, 2024

Thank you for your response. We have been able to successfully create a test collection and update it to include __pclass. That test class did then work the way we are accustomed to using our Mongolid models.

We are concerned about implementation in one area. Will you please address it? How should we implement a class type for classes that sometimes (but not always) extend other classes (inheritance)?

As an example, we created a collection called 'products'. Its namespace is MyApp\Catalog\Product. We then created a new website, using the same products collection, but that has a different getPrice() method so it could use different rules for the different site. So we created MyApp\Template\Product, which 'extends' MyApp\Catalog\Product and has only one method in it, but benefits from the methods on its parent class. We add the Template directory after the other directories in our auto-loader, so those classes get loaded instead of the base classes. We can then call either class, based on whether the Template namespace exists, by calling App::make("Product").

Can we add an alias instead of a namespace? How can we address class inheritance?

from mongolid-laravel.

ravanscafi avatar ravanscafi commented on July 21, 2024

Awesome! I was wondering if you managed to use it :)
There is a little friction migrating from v2, but it is worth it.

About "inheritance", we do have a concept that I think might work for you. It's what we call PolymorphableModel. Please take a look at PolymorphableModelInterface.

Basically, you make your base Product implement it, creating a polymorph model which will tell which class mongolid must use for your model. There are some tests that might help understanding the concept.

We really need to update the documentation 😉

Please tell me about your progress!

from mongolid-laravel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.