JT2809
JT2809

Reputation: 11

Strategies with mongodb-odm / handle deleted references

We're using doctrine/mongodb-odm in our project and there is a model with references to another model:

@ReferenceMany(targetDocument="Extra", simple=true)

When we retrieve the referenced objects it's possible that one of the objects isn't available anymore. So we have added the sort attribute to ensure that a db query will be executed (deleted objects are ignored):

@ReferenceMany(targetDocument="Extra", simple=true, sort={"name"="asc"})

With doctrine/mongodb-odm 1.0.3 it isn't possible anymore and breaks with Doctrine\ODM\MongoDB\Mapping\MappingException:

ReferenceMany's sort can not be used with addToSet and pushAll strategies, pushAll used in Example::extras

Why I can't use "sort" with these strategies? Are strategies not only interesting for write requests?

Is it possible to use the set strategy to keep the same application logic?

@ReferenceMany(targetDocument="Extra", simple=true, strategy="set", sort={"name"="asc"})

Is the only difference, when I add a further reference, all existing references will completely rewritten?

Upvotes: 1

Views: 354

Answers (1)

malarzm
malarzm

Reputation: 2966

Why I can't use "sort" with these strategies? Are strategies not only interesting for write requests?

Yes, strategies are mostly for relevant for writing collections, but in this case your collection is not the inverse-side of reference hence it is tracking changes to itself. Strategies like addToSet and pushAll are doing positional updates later and with sort applied order in document may differ order in database which in turn may lead to data loss when saving such collection.

Is it possible to use the set strategy to keep the same application logic?

Yes, application logic will not change. Also you may want to use setArray strategy to ensure BSON array is saved to database or, better yet, atomicSetArray to ensure only one query is fired to update both documents and references.

Is the only difference, when I add a further reference, all existing references will completely rewritten?

Generally speaking, yes.

Upvotes: 1

Related Questions