Skip to content

Commit 3f5b1dc

Browse files
authored
Merge pull request #1953 from divine/pr_1745
[Updated PR#1745] Bugfix create collection with options
2 parents 6cea8aa + ae88c82 commit 3f5b1dc

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,18 @@ public function expire($columns, $seconds)
234234
}
235235

236236
/**
237-
* @inheritdoc
237+
* Indicate that the collection needs to be created.
238+
* @param array $options
239+
* @return void
238240
*/
239-
public function create()
241+
public function create($options = [])
240242
{
241243
$collection = $this->collection->getCollectionName();
242244

243245
$db = $this->connection->getMongoDB();
244246

245247
// Ensure the collection is created.
246-
$db->createCollection($collection);
248+
$db->createCollection($collection, $options);
247249
}
248250

249251
/**

src/Jenssegers/Mongodb/Schema/Builder.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ public function hasColumns($table, array $columns)
3333

3434
/**
3535
* Determine if the given collection exists.
36-
* @param string $collection
36+
* @param string $name
3737
* @return bool
3838
*/
39-
public function hasCollection($collection)
39+
public function hasCollection($name)
4040
{
4141
$db = $this->connection->getMongoDB();
4242

43-
foreach ($db->listCollections() as $collectionFromMongo) {
44-
if ($collectionFromMongo->getName() == $collection) {
45-
return true;
46-
}
47-
}
43+
$collections = iterator_to_array($db->listCollections([
44+
'filter' => [
45+
'name' => $name,
46+
],
47+
]), false);
4848

49-
return false;
49+
return count($collections) ? true : false;
5050
}
5151

5252
/**
@@ -134,6 +134,24 @@ protected function createBlueprint($collection, Closure $callback = null)
134134
return new Blueprint($this->connection, $collection);
135135
}
136136

137+
/**
138+
* Get collection.
139+
* @param string $name
140+
* @return bool|\MongoDB\Model\CollectionInfo
141+
*/
142+
public function getCollection($name)
143+
{
144+
$db = $this->connection->getMongoDB();
145+
146+
$collections = iterator_to_array($db->listCollections([
147+
'filter' => [
148+
'name' => $name,
149+
],
150+
]), false);
151+
152+
return count($collections) ? current($collections) : false;
153+
}
154+
137155
/**
138156
* Get all of the collections names for the database.
139157
* @return array

tests/SchemaTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function testCreateWithOptions(): void
3434
Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
3535
$this->assertTrue(Schema::hasCollection('newcollection_two'));
3636
$this->assertTrue(Schema::hasTable('newcollection_two'));
37+
38+
$collection = Schema::getCollection('newcollection_two');
39+
$this->assertTrue($collection['options']['capped']);
40+
$this->assertEquals(1024, $collection['options']['size']);
3741
}
3842

3943
public function testDrop(): void

0 commit comments

Comments
 (0)