public prepareInsertData ( mixed $document ) : array | ||
$document | mixed | |
return | array |
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;
}
/** * Executes all queued document insertions. * * Queued documents without an ID will inserted in a batch and queued * documents with an ID will be upserted individually. * * If no inserts are queued, invoking this method is a NOOP. * * @param array $options Options for batchInsert() and update() driver methods */ public function executeInserts(array $options = array()) { if (!$this->queuedInserts) { return; } $inserts = array(); foreach ($this->queuedInserts as $oid => $document) { $data = $this->pb->prepareInsertData($document); // Set the initial version for each insert if ($this->class->isVersioned) { $versionMapping = $this->class->fieldMappings[$this->class->versionField]; if ($versionMapping['type'] === 'int') { $nextVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); } elseif ($versionMapping['type'] === 'date') { $nextVersionDateTime = new \DateTime(); $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); } $data[$versionMapping['name']] = $nextVersion; } $inserts[$oid] = $data; } if ($inserts) { try { $this->collection->batchInsert($inserts, $options); } catch (\MongoException $e) { $this->queuedInserts = array(); throw $e; } } $this->queuedInserts = array(); }