I got this structure for my game Document
Game Document.
{ "_id" : ObjectId( "51043d468ead0e0757000006" ),
"active" : true,
"created" : 1359232326,
"difficulty" : 0,
"map" : { "$ref" : "Map",
"$id" : ObjectId( "5103c0228ead0e3764000000" ),
"$db" : "mydb" },
"mode" : "coop",
"players" : [
{ "$ref" : "User",
"$id" : ObjectId( "50d83abf038054b560000000" ),
"$db" : "mydb" } ],
"title" : "testgame" }
Now i want to add another player to this Game, this is what i’ve tried
$user = $this->get('doctrine_mongodb')
->getRepository('FantasytdUserBundle:User')
->findById($userId);
$db->createQueryBuilder()
->update()
->field('id')->equals($gameId)
->field('players')->push($user)
->getQuery()
->execute();
Also
$user = array_pop($user->toArray());
No luck, all i get is
“zero-length keys are not allowed, did you use $ with double quotes?”
In my doctrine document the Players is defined to be a reference to User.
AFAIK doctrine 2 can’t push documents. You should prepare array:
Try something like this: