Skip to content

Why MongoDate is converted to string ? #64

Closed
@alexandre-butynski

Description

@alexandre-butynski

The default behavior is to convert MongoDate to string after the requests.

public function setRawAttributes(array $attributes, $sync = false)
{
    foreach($attributes as $key => &$value)
    {
        // Convert MongoId to string
        if ($value instanceof MongoId)
        {
            $value = (string) $value;
        }

        // Convert MongoDate to string
        else if ($value instanceof MongoDate)
        {
            $value = $this->asDateTime($value)->format('Y-m-d H:i:s');
        }
    }

    parent::setRawAttributes($attributes, $sync);
}

Why ? If we save a date as a MongoDate, it is to be able to make operation on this field. For example, detect the number of days since a start date.

Today, we have very strange cases like this one :

$program = Program::create('date_start' => new MongoDate());

var_dump($program->date_start);
var_dump(Program::find($program->id)->date_start);
// Output :
class MongoDate#2497 (2) {
  public $sec =>
  int(1384179623)
  public $usec =>
  int(0)
}
string(19) "2013-11-11 14:20:23"

And a default string format is not very useful because it is different for each country (I'm french...). Something more useful could be to override the MongoDate __toString method but I'm not sure that it is the role of this package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions