/** * If the document is new, ignore shard key field value, otherwise throw an exception. * Also, shard key field should be present in actual document data. * * @param object $document * @param string $shardKeyField * @param array $actualDocumentData * * @throws MongoDBException */ private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) { $dcs = $this->uow->getDocumentChangeSet($document); $isUpdate = $this->uow->isScheduledForUpdate($document); $fieldMapping = $this->class->getFieldMappingByDbFieldName($shardKeyField); $fieldName = $fieldMapping['fieldName']; if ($isUpdate && isset($dcs[$fieldName]) && $dcs[$fieldName][0] != $dcs[$fieldName][1]) { throw MongoDBException::shardKeyFieldCannotBeChanged($shardKeyField, $this->class->getName()); } if (!isset($actualDocumentData[$fieldName])) { throw MongoDBException::shardKeyFieldMissing($shardKeyField, $this->class->getName()); } }