cwhisperer
cwhisperer

Reputation: 1926

Doctrine2 mongodb and Timestamp

I use doctrine 2 to write to mongodb and an Entity looks like this :

{
  "_id" : ObjectId("54538be2516bd636738b46f2"),
  "department" : "Assistance",
  "action" : "DONE",
  "idAppointment" : 2197,
  "service_order" : "F4889109",
  "access_number" : "164016178",
  "so_type" : "DERA",
  "stage" : "D40",
  "id_va" : "FT452926",
  "id_icms" : "F4889109",
  "ref_isp" : "",
  "comment" : "",
  "first_available_schedule" : Timestamp(1414623600, 168),
  "asap" : false,
  "allday" : "NO",
  "operator" : "HD8",
  "created" : Timestamp(1414761442, 169)
}

Now I want to make a query on the timestamp either with querybuilder or on the command line but I don't know exactly how to do this. Any help will be appreciated.

Upvotes: 0

Views: 634

Answers (1)

jmikola
jmikola

Reputation: 6922

For starters, you appear to be using the Timestamp BSON type when you should actually be using the Date type. In ODM, these use the @Timestamp and @Date annotations, respectively. In the PHP driver, the relevant driver classes are MongoTimestamp and MongoDate, respectively.

As the BSON documentation link above states, Timestamp is intended for internal use (MongoDB uses it in the replication oplog). If you're storing dates and times in your application, the Date time is certainly what you should use.

Looking at the @Timestamp example in ODM's documentation, it's rather unfortunate that the example has it on a $created field. We would do better to use a less encouraging field name and add a note that users probably don't want to map a field as @Timestamp.

If you do have fields mapped as @Date, you should be able to use the query builder as you would with any other field and operator (relevant to a date value). When preparing the query, DocumentPersister::prepareQueryOrNewObj() will ultimately run the query value corresponding to a date-mapped field through DateType::convertToDatabaseValue(). This means that the value may be an integer timestamp, DateTime, or MongoDate instance. A string would also be suitable, provided it works with strtotime().

Upvotes: 1

Related Questions