Skip to content

Commit

Permalink
Extract GridFS bucket name to separate property
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alcaeus committed Jun 8, 2018
1 parent a4a4df6 commit 35be2ef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -2195,6 +2212,7 @@ public function __sleep()

if ($this->isFile) {
$serialized[] = 'isFile';
$serialized[] = 'bucketName';
}

if ($this->isVersioned) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 35be2ef

Please sign in to comment.