/**
  * Inserts new rows for a PersistentCollection instance.
  *
  * @param PersistentCollection $coll
  * @param array $options
  */
 private function insertRows(PersistentCollection $coll, array $options)
 {
     $mapping = $coll->getMapping();
     list($propertyPath, $parent) = $this->getPathAndParent($coll);
     if ($mapping['strategy'] === 'set') {
         $setData = array();
         foreach ($coll as $document) {
             if (isset($mapping['reference'])) {
                 $setData[] = $this->pb->prepareReferencedDocumentValue($mapping, $document);
             } else {
                 $setData[] = $this->pb->prepareEmbeddedDocumentValue($mapping, $document);
             }
         }
         $query = array($this->cmd . 'set' => array($propertyPath => $setData));
         $this->executeQuery($parent, $query, $options);
     } else {
         $strategy = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll';
         $insertDiff = $coll->getInsertDiff();
         if ($insertDiff) {
             $query = array($this->cmd . $strategy => array());
             foreach ($insertDiff as $key => $document) {
                 if (isset($mapping['reference'])) {
                     $query[$this->cmd . $strategy][$propertyPath][] = $this->pb->prepareReferencedDocumentValue($mapping, $document);
                 } else {
                     $query[$this->cmd . $strategy][$propertyPath][] = $this->pb->prepareEmbeddedDocumentValue($mapping, $document);
                 }
             }
             $this->executeQuery($parent, $query, $options);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @param $document
  * @return array
  * @throws MongoDBException
  */
 public function getShardKeyQuery($document)
 {
     $shardKeysQueryPart = array();
     $dcs = $this->uow->getDocumentChangeSet($document);
     $data = $this->uow->getDocumentActualData($document);
     $md = $this->dm->getMetadataFactory()->getMetadataFor(get_class($document));
     $keys = $md->shardKeys;
     $fieldMappings = $this->dm->getClassMetadata(get_class($document))->fieldMappings;
     foreach ($keys as $key) {
         if ($key !== 'id') {
             $queryKey = $fieldMappings[$key]['name'];
         } else {
             $queryKey = '_id';
         }
         //If the document is new, we can ignore shard key value, otherwise throw exception
         $isUpdate = $this->uow->isScheduledForUpdate($document);
         if ($isUpdate && isset($dcs[$key]) && $dcs[$key][0] !== null && $dcs[$key][0] != $dcs[$key][1] && (!isset($options['upsert']) || isset($options['upsert']) && $options['upsert'] === true)) {
             throw MongoDBException::shardKeyChange($key);
         }
         if (!isset($data[$key])) {
             throw MongoDBException::shardKeyMissing($key);
         }
         $new = $data[$key];
         $mapping = $fieldMappings[$key];
         // @Field, @String, @Date, etc.
         if (!isset($mapping['association'])) {
             if (isset($new) || $mapping['nullable'] === true) {
                 $shardKeysQueryPart[$queryKey] = is_null($new) ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
             }
             // @ReferenceOne
         } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
             if (isset($new) || $mapping['nullable'] === true) {
                 $shardKeysQueryPart[$queryKey] = is_null($new) ? null : $this->pb->prepareReferencedDocumentValue($mapping, $new);
             }
             // @ReferenceMany
         } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_MANY) {
             // Do nothing right now
         }
     }
     return $shardKeysQueryPart;
 }