Skip to content

Why MongoDate is converted to string ? #64

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

Closed
alexandre-butynski opened this issue Nov 12, 2013 · 8 comments
Closed

Why MongoDate is converted to string ? #64

alexandre-butynski opened this issue Nov 12, 2013 · 8 comments

Comments

@alexandre-butynski
Copy link
Contributor

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.

@jenssegers
Copy link
Contributor

If you want to work with "custom" date fields, you need to specify them in your model like you would with the original Eloquent model. https://github.com/jenssegers/Laravel-MongoDB#dates

@alexandre-butynski
Copy link
Contributor Author

Thank's for your fast answer. I have missed a part of the story !

The following line solve my issue :

protected $dates = array('date_start');

So, it's not very important to me now, but why convert MongoDate to string ? It could be the role of a custom getter, to let everyone choose its own output format.

(I let you close the issue if it's ok for you).

@jenssegers
Copy link
Contributor

The reason I convert it to a string is mainly for the JSON output of the model. But it should not really matter if you specify the date fields in the model class.

@rcwsr
Copy link

rcwsr commented Nov 25, 2013

Why does the documentation say "Internally, these dates will be converted to MongoDate objects when saved to the database."?

@jenssegers
Copy link
Contributor

@robcaw, only if you specify them in your class: protected $dates = array('birthday');

@rcwsr
Copy link

rcwsr commented Nov 25, 2013

Oh right. I have protected $dates = array('dob'); in my User class, but
as far as I can see, it is stored as a string in the db.

On Mon, Nov 25, 2013 at 7:22 PM, Jens Segers [email protected]:

@robcaw https://github.com/robcaw, only if you specify them in your
class: protected $dates = array('birthday');


Reply to this email directly or view it on GitHubhttps://github.com//issues/64#issuecomment-29232861
.

@jenssegers
Copy link
Contributor

That should not happen. This functionality is tested in https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/ModelTest.php#L312

@rcwsr
Copy link

rcwsr commented Nov 25, 2013

Nevermind, I hadn't saved the date string as a DateTime object when creating my User. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants