Reputation: 135
When I add new record, date saves as a String:
"created_at" : "0.67200000 1407770545"
My question is how to save date as an object? I'm expecting to see something like this:
"created_at" : ISODate("2013-09-22T07:41:44.451Z"),
My code:
class User
{
/**
* @MongoDB\Date
* @MongoDB\Field(name="created_at")
*/
protected $createdAt;
/**
* @param MongoDB\Date $createdAt
* @return self
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return MongoDB\Date $createdAt
*/
public function getCreatedAt()
{
return $this->createdAt;
}
}
$user= new User();
$user->setCreatedAt(new \MongoDate());
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($user);
$dm->flush();
Upvotes: 2
Views: 743
Reputation: 135
It works fine with @MongoDB\Date
annotation only.
When second annotation @MongoDB\Field(name="created_at")
is added it doesn't save date with correct type. The reason is that @MongoDB\Field
annotation requires to specify data type.
The problem was fixed by changing annotations from:
/**
* @MongoDB\Date
* @MongoDB\Field(name="created_at")
*/
to:
/**
* @MongoDB\Field(name="created_at", type="date")
*/
Upvotes: 1