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

Replace increment type with a storage strategy #1352

Closed
wants to merge 4 commits into from

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Jan 27, 2016

This PR deprecates the increment type that can be used in ODM, replacing it instead with a more flexible strategy option for fields.

First of all, the increment type was supposed to handle int and float values at the same time. There was no option to ensure a value read from the database was cast to an int (which could lead to unexpected floats appearing in the application if one wasn't careful).
Furthermore, for collections there is a strategy option which controls how the elements in a collection are written to the database. This kind of write control is exactly what should be used for increment fields as well.

At first glance, there are no BC breaks to this. When the ClassMetadataInfo class encounters a mapping with type increment, it will automatically set the strategy to increment, regardless of any other strategy being set. The type is left unchanged to keep the current type cast behavior. Users are encouraged to switch their increment mappings to the new strategy field.

Open tasks:

  • Write proper documentation about the new strategy, add deprecation notes for the increment type
  • Collection strategies still use magic strings, these should be converted to class constants for good measure
  • The validation which strategy is acceptable for which mapping should probably be generalized and put in one place to ensure consistency.

@malarzm: I'd appreciate your guidance on the last two tasks since you've spent quite some time with collections ;)

@alcaeus alcaeus added this to the 1.1 milestone Jan 27, 2016
@@ -64,7 +64,7 @@
<xs:attribute name="id" type="xs:boolean" default="false" />
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:attribute name="type" type="xs:NMTOKEN" default="string" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="pushAll" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="set" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, got it after looking further :) Won't it break collections though? (note: I'm n00b when it comes to XMLs and theiir stuff)

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 type only applies to scalar fields, the association types have a different mapping. Technically, this field was not used to begin with ;)

@malarzm
Copy link
Member

malarzm commented Jan 28, 2016

Like the approach and where it can go :) I'll elaborate on later today

* @param string $strategy
* @return MappingException
*/
public static function invalidStorageStrategy($className, $fieldName, $strategy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pass type here as well, optionally we could also throw in allowed strategies and propose correct one basing on Levenshtein distance? But it can be too much :D

@malarzm
Copy link
Member

malarzm commented Jan 28, 2016

Collection strategies still use magic strings, these should be converted to class constants for good measure

ClassMetadataInfo::STORAGE_STRATEGY_ATOMIC_SET_ARRAY? I'd pass on this :) Also it's easier to write atomicSetArray in mapping than calling constant I think. Also for internal usage they're pretty much hidden by CollectionHelper

The validation which strategy is acceptable for which mapping should probably be generalized and put in one place to ensure consistency.

👍 on this, end of ClassMetadataInfo::mapField seems like a good place for this especially there are no early returns. We could put some switch over there that would check certain types if their strategy is allowed (and throw exception if not) and by default unset whatever was passed there.

@alcaeus alcaeus force-pushed the deprecate-increment-type branch from 2d739bb to 9256261 Compare January 30, 2016 09:56

The ``increment`` strategy does not apply to collections but can be used for
``int`` and ``float`` fields. When using the ``increment`` strategy, the field
value will be updated using an `$inc`_ operator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can write "the $inc operator" here.

@alcaeus alcaeus force-pushed the deprecate-increment-type branch from a0c8d7c to 37ca9a4 Compare February 6, 2016 20:38
Doctrine MongoDB ODM implements four different strategies for persisting changes
to collections of embedded documents or references. These strategies apply to
the following mapping types:
Doctrine MongoDB ODM implements seven different strategies for persisting changes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing seven to several? I noticed "four" only lately and haven't got to changing it to "six", I suppose it'll be the same with "seven" when we add eighth one :D

This removes a bunch of magic strings that were used throughout.
This also forces all EmbedOne and ReferenceOne mappings to use the set strategy. All other strategies will throw an error.
This changes the collection strategy documentation to be more generic and describe all storage strategies, regardless of whether they apply to collections or not.
@alcaeus alcaeus force-pushed the deprecate-increment-type branch from 37ca9a4 to b24d6af Compare February 7, 2016 17:41
malarzm added a commit that referenced this pull request Feb 20, 2016
@malarzm
Copy link
Member

malarzm commented Feb 20, 2016

Merged manually in b1cbaf8

@malarzm malarzm closed this Feb 20, 2016
@alcaeus alcaeus deleted the deprecate-increment-type branch March 30, 2016 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants