public function prepareInsertData($document)
{
$oid = spl_object_hash($document);
$class = $this->dm->getClassMetadata(get_class($document));
$changeset = $this->uow->getDocumentChangeSet($document);
$insertData = array();
foreach ($class->fieldMappings as $mapping) {
// many collections are inserted later
if ($mapping['type'] === 'many') {
continue;
}
// skip not saved fields
if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
continue;
}
// Skip version and lock fields
if (isset($mapping['version']) || isset($mapping['lock'])) {
continue;
}
$new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
// Prepare new document identifier
if ($class->isIdentifier($mapping['fieldName'])) {
if ( ! $class->isIdGeneratorNone() && $new === null) {
$new = $class->idGenerator->generate($this->dm, $document);
}
$insertData['_id'] = Type::getType($mapping['type'])->convertToDatabaseValue($new);
continue;
}
// Skip null values
if ($new === null && $mapping['nullable'] === false) {
continue;
}
$value = $this->prepareValue($mapping, $new);
// Check if a reference is not persisted yet and we need to schedule an extra update
$insertData[$mapping['name']] = $value;
if (isset($mapping['reference'])) {
$scheduleForUpdate = false;
if ($mapping['type'] === 'one') {
if ( ! isset($insertData[$mapping['name']][$this->cmd . 'id'])) {
$scheduleForUpdate = true;
}
}
if ($scheduleForUpdate) {
unset($insertData[$mapping['name']]);
$this->uow->scheduleExtraUpdate($document, array(
$mapping['fieldName'] => array(null, $new)
));
}
}
}
// add discriminator if the class has one
if ($class->hasDiscriminator()) {
$insertData[$class->discriminatorField['name']] = $class->discriminatorValue;
}
return $insertData;
}