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

Embedded Subset Pattern implementation #2608

Open
etshy opened this issue Jan 20, 2024 · 2 comments
Open

Embedded Subset Pattern implementation #2608

etshy opened this issue Jan 20, 2024 · 2 comments
Labels

Comments

@etshy
Copy link
Contributor

etshy commented Jan 20, 2024

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

I learned about this pattern when searching about when/what to embed etc. and I think it could be a great addition to doctrine-odm.
I'm not sure how it could be implemented yet but having an Attribute #[SubSet(number:5, sort: DESC)] in addition to an attribute #[ReferenceMany] to store the 5 most recents referenced directly on an embed could reduce read operation on the db.

I learned about this pattern on the mongodb website here and here

@malarzm
Copy link
Member

malarzm commented Jan 20, 2024

I believe this resembles #2252 and also suffers the same challenge: tracking changes. ODM would need to know that a (let's say) review has been changed and update its product accordingly.

Also, while less sexy than an attribute, the pattern is already doable and quite elegant:

class Product
{
    #[ReferenceMany(targetClass: Review::class)]
    private Collection $reviews;

    #[EmbedMany(targetClass: PartialReview::class)]
    private Collection $recentReviews;

    public function addReview(Review $review)
    {
        $this->reviews[] = $review;
        $this->recentReviews[] = new PartialReview($review);
        // maybe pop one last recent reviews if there's too many
    }
}

You could even hide capping recent reviews with a custom collection :)

@etshy
Copy link
Contributor Author

etshy commented Jan 31, 2024

Yeah tracking changes and knowing when to fetch the whole references list is what made me think about the most and couldn't think any good solutions for that.

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

No branches or pull requests

2 participants