Skip to content

Instantly share code, notes, and snippets.

@geraldcroes
Created August 12, 2013 16:17
Show Gist options
  • Save geraldcroes/6212423 to your computer and use it in GitHub Desktop.
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
<?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