Created
August 12, 2013 16:17
-
-
Save geraldcroes/6212423 to your computer and use it in GitHub Desktop.
Mongo insert bug with the magic add a _id even if the data is passed by copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class EntityObject { | |
private $arData = array(); | |
public function __construct ($initialValue) { | |
$this->setProperty($initialValue); | |
} | |
public function setProperty ($value) { | |
$this->arData['property'] = $value; | |
} | |
public function getProperties () { | |
return $this->arData; | |
} | |
} | |
$entity = new EntityObject('foo'); | |
$mongoClient = new MongoClient(); | |
$toInsert = $entity->getProperties(); | |
$mongoClient->fooDb->fooCollection->insert($toInsert, array('fsync'=>true)); | |
echo "toInsert should be updated with _id \n"; | |
print_r($toInsert); | |
echo "private data should not be updated with _id \n"; | |
print_r($entity->getProperties()); | |
/* | |
Expected result : | |
toInsert should be updated with _id | |
Array | |
( | |
[property] => foo | |
[_id] => MongoId Object | |
( | |
[$id] => 52090a28945630cf261e34fc | |
) | |
) | |
private data should not be updated with _id | |
Array | |
( | |
[property] => foo | |
) | |
Actual result | |
toInsert should be updated with _id | |
Array | |
( | |
[property] => foo | |
[_id] => MongoId Object | |
( | |
[$id] => 52090a28945630cf261e34fc | |
) | |
) | |
private data should not be updated with _id | |
Array | |
( | |
[property] => foo | |
[_id] => MongoId Object | |
( | |
[$id] => 52090a28945630cf261e34fc | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment