/**
     * Prepares insert data for document
     *
     * @param mixed $document
     * @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;
    }
Ejemplo n.º 2
0
 /**
  * Prepares the array that is ready to be inserted to mongodb for a given object document.
  *
  * @param object $document
  * @return array $insertData
  */
 public function prepareInsertData($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'] === ClassMetadata::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;
         // Generate a document identifier
         if ($new === null && $class->identifier === $mapping['fieldName'] && $class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE) {
             $new = $class->idGenerator->generate($this->dm, $document);
         }
         // Don't store null values unless nullable === true
         if ($new === null && $mapping['nullable'] === false) {
             continue;
         }
         $value = null;
         if ($new !== null) {
             // @Field, @String, @Date, etc.
             if (!isset($mapping['association'])) {
                 $value = Type::getType($mapping['type'])->convertToDatabaseValue($new);
                 // @ReferenceOne
             } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
                 if ($mapping['isInverseSide']) {
                     continue;
                 }
                 $oid = spl_object_hash($new);
                 if ($this->isScheduledForInsert($new)) {
                     // The associated document $new is not yet persisted, so we must
                     // set $new = null, in order to insert a null value and schedule an
                     // extra update on the UnitOfWork.
                     $this->uow->scheduleExtraUpdate($document, array($mapping['fieldName'] => array(null, $new)));
                 } else {
                     $value = $this->prepareReferencedDocumentValue($mapping, $new);
                 }
                 // @EmbedOne
             } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
                 $value = $this->prepareEmbeddedDocumentValue($mapping, $new);
                 // @ReferenceMany
             } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_MANY) {
                 $value = array();
                 foreach ($new as $reference) {
                     $value[] = $this->prepareReferenceDocValue($mapping, $reference);
                 }
                 // @EmbedMany
             } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_MANY) {
                 $value = array();
                 foreach ($new as $reference) {
                     $value[] = $this->prepareEmbeddedDocumentValue($mapping, $reference);
                 }
             }
         }
         $insertData[$mapping['name']] = $value;
     }
     // add discriminator if the class has one
     if ($class->hasDiscriminator()) {
         $insertData[$class->discriminatorField['name']] = $class->discriminatorValue;
     }
     return $insertData;
 }