Commit 2dcfa5b5 authored by Ditty's avatar Ditty

Add getCollection function and little improvements

Add getCollection function and little improvements
parent bf1e25ae
...@@ -33,21 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder ...@@ -33,21 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder
/** /**
* Determine if the given collection exists. * Determine if the given collection exists.
* @param string $collection * @param string $name
* @param bool $collection_info * @return bool
* @return bool|\MongoDB\Model\CollectionInfo
*/ */
public function hasCollection($collection, $collection_info = false) public function hasCollection($name)
{ {
$db = $this->connection->getMongoDB(); $db = $this->connection->getMongoDB();
foreach ($db->listCollections() as $collectionFromMongo) { $collections = $db->listCollections([
if ($collectionFromMongo->getName() == $collection) { 'filter' => [
return $collection_info ? $collectionFromMongo : true; 'name' => $name,
} ],
} ]);
return false; return $collections->count() ? true : false;
} }
/** /**
...@@ -135,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder ...@@ -135,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder
return new Blueprint($this->connection, $collection); return new Blueprint($this->connection, $collection);
} }
/**
* Get collection.
* @param string $name
* @return bool|\MongoDB\Model\CollectionInfo
*/
public function getCollection($name)
{
$db = $this->connection->getMongoDB();
$collections = $db->listCollections([
'filter' => [
'name' => $name,
],
]);
return $collections->count() ? (($collections = iterator_to_array($collections) ) ? current($collections) : false) : false;
}
/** /**
* Get all of the collections names for the database. * Get all of the collections names for the database.
* @return array * @return array
......
...@@ -35,7 +35,7 @@ class SchemaTest extends TestCase ...@@ -35,7 +35,7 @@ class SchemaTest extends TestCase
$this->assertTrue(Schema::hasCollection('newcollection_two')); $this->assertTrue(Schema::hasCollection('newcollection_two'));
$this->assertTrue(Schema::hasTable('newcollection_two')); $this->assertTrue(Schema::hasTable('newcollection_two'));
$collection = Schema::hasCollection('newcollection_two', true); $collection = Schema::getCollection('newcollection_two');
$this->assertTrue($collection['options']['capped']); $this->assertTrue($collection['options']['capped']);
$this->assertEquals(1024, $collection['options']['size']); $this->assertEquals(1024, $collection['options']['size']);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment