diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index a8c79a8768..38335ab4c7 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -372,16 +372,16 @@ public function getDocumentBucket(string $className): Bucket throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className); } - $collectionName = $metadata->getCollection(); + $bucketName = $metadata->getBucketName(); - if (! $collectionName) { + if (! $bucketName) { throw MongoDBException::documentNotMappedToCollection($className); } if (! isset($this->documentBuckets[$className])) { $db = $this->getDocumentDatabase($className); - $options = ['bucketName' => $collectionName]; + $options = ['bucketName' => $bucketName]; if ($metadata->readPreference !== null) { $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); } diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index a9b54f59d6..ba56551e37 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -187,6 +187,12 @@ class ClassMetadata implements BaseClassMetadata */ public $collection; + /** + * READ-ONLY: The name of the GridFS bucket the document is mapped to. + * @var string + */ + public $bucketName; + /** * READ-ONLY: If the collection should be a fixed size. * @var bool @@ -1159,6 +1165,17 @@ public function setCollection($name) } } + public function getBucketName(): ?string + { + return $this->bucketName; + } + + public function setBucketName(string $bucketName): void + { + $this->bucketName = $bucketName; + $this->setCollection($bucketName . '.files'); + } + /** * Get whether or not the documents collection is capped. * @@ -2195,6 +2212,7 @@ public function __sleep() if ($this->isFile) { $serialized[] = 'isFile'; + $serialized[] = 'bucketName'; } if ($this->isVersioned) { diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php index 7ba45fa538..5b3bad94a3 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php @@ -108,7 +108,7 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma } // Store bucketName as collection name for GridFS files if (isset($documentAnnot->bucketName)) { - $class->setCollection($documentAnnot->bucketName); + $class->setBucketName($documentAnnot->bucketName); } if (isset($documentAnnot->repositoryClass)) { $class->setCustomRepositoryClass($documentAnnot->repositoryClass); diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php index 21c6c6c309..c5d38ffd0b 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php @@ -109,7 +109,7 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma } } if (isset($xmlRoot['bucket-name'])) { - $class->setCollection((string) $xmlRoot['bucket-name']); + $class->setBucketName((string) $xmlRoot['bucket-name']); } if (isset($xmlRoot['writeConcern'])) { $class->setWriteConcern((string) $xmlRoot['writeConcern']); diff --git a/lib/Doctrine/ODM/MongoDB/SchemaManager.php b/lib/Doctrine/ODM/MongoDB/SchemaManager.php index b2605e1d0a..ef43abebfd 100644 --- a/lib/Doctrine/ODM/MongoDB/SchemaManager.php +++ b/lib/Doctrine/ODM/MongoDB/SchemaManager.php @@ -319,8 +319,8 @@ public function createDocumentCollection($documentName) } if ($class->isFile) { - $this->dm->getDocumentDatabase($documentName)->createCollection($class->getCollection() . '.files'); - $this->dm->getDocumentDatabase($documentName)->createCollection($class->getCollection() . '.chunks'); + $this->dm->getDocumentDatabase($documentName)->createCollection($class->getBucketName() . '.files'); + $this->dm->getDocumentDatabase($documentName)->createCollection($class->getBucketName() . '.chunks'); return; }