Convert DateTime to MongoDate within Hash types #421

defrag opened this Issue Oct 1, 2012 · 4 comments


None yet

3 participants

defrag commented Oct 1, 2012

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 commented Oct 1, 2012

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

jmikola commented Oct 1, 2012

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 commented Oct 1, 2012

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 commented Oct 11, 2012

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