From 35be2ef06441b097ad45751062aa70f24bd15759 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 18 May 2018 07:26:41 +0200 Subject: [PATCH] Extract GridFS bucket name to separate property This removes the dual-use from the collection property in ClassMetadata, storing the bucket name for GridFS files in the bucketName property. For compatibility, the collection name is initialized to the fs.files collection for the corresponding bucket. --- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 6 +++--- .../ODM/MongoDB/Mapping/ClassMetadata.php | 18 ++++++++++++++++++ .../Mapping/Driver/AnnotationDriver.php | 2 +- .../ODM/MongoDB/Mapping/Driver/XmlDriver.php | 2 +- lib/Doctrine/ODM/MongoDB/SchemaManager.php | 4 ++-- 5 files changed, 25 insertions(+), 7 deletions(-) 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; }