Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Convert DateTime to MongoDate within Hash types #421

Open
defrag opened this Issue · 4 comments

3 participants

@defrag

Hello,
currently there isnt possiblity to store DateTime fields in Hash Type, cuz it always casts
to array. Would it make sense to be able to store DateTime fields in Hash?
If i could get liitle guidence how to change it, I would gladly do it.

@defrag

Well it works with MongoDate, so not sure if converting from DateTime would be needed.

@jmikola
Owner

Is the DateTime a value within the associative array, and that array is being mapped as a hash field in ODM? I just want to make sure that you're not trying to store a DateTime instance as a hash field, and the DateTime is being casted to an array.

The translation of DateTime to/from MongoDate only occurs due to logic within DateType. The HashType does not process values within the array; it simply casts to/from an array.

If you require date conversion in a nested field, you may want to consider using embedded documents; however, if you are relying on the hash field to avoid mapping the inner fields, you'll also need to make sure your values are in a format suitable for the Mongo driver (e.g. using MongoDate instead of DateTime).

@defrag

No no, im not trying to store DateTime instance as a hash field, I store it as value. For example
$mailingsSent is mapped hash and i wanted to add $mailingsSent[$key] = new \DateTime, but it was saved as array.

$t[$key] = new \MongoDate($timestamp) works fine and solves my issue so i think we can close this issue.

@jwage
Owner

Currently @Hash is a raw PHP array that is passed directly to mongodb and not prepared. So if we wanted to support this we'd have to start processing hash values and converting values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.