Get database instance
public getDatabase ( string $name = null ) : |
||
$name | string | database name |
리턴 |
public function setUp() { // connect to mongo $client = new Client(getenv('PHPMONGO_DSN') ? getenv('PHPMONGO_DSN') : null); // select database $database = $client->getDatabase('test'); // select collection $this->collection = $database->getCollection('phpmongo_test_collection'); }
public function setUp() { // connect to mongo $client = new Client(); // select database $database = $client->getDatabase('test'); // select collection $this->collection = $database->getCollection('phpmongo_test_collection'); }
public function testMapCollectionToClassPrefix() { $this->client->map(array('db1' => array('db1Collection1' => '\\Sokil\\Mongo\\Db1Collection1Class', 'db1Collection2' => '\\Sokil\\Mongo\\Db1Collection2Class'), 'db2' => '\\Sokil\\Mongo\\')); $database = $this->client->getDatabase('db2'); $reflectionClas = new \ReflectionClass($database); $method = $reflectionClas->getMethod('getCollectionDefinition'); $method->setAccessible(true); $classDefinition = $method->invoke($database, 'db1Collection2Class'); $this->assertEquals('\\Sokil\\Mongo\\Db1Collection2Class', $classDefinition->class); }
/** * Copy selected documents to another collection * * @param type $targetCollectionName * @param type $targetDatabaseName Target database name. If not specified - use current */ public function copyToCollection($targetCollectionName, $targetDatabaseName = null) { // target database if (!$targetDatabaseName) { $database = $this->collection->getDatabase(); } else { $database = $this->client->getDatabase($targetDatabaseName); } // target collection $targetMongoCollection = $database->getCollection($targetCollectionName)->getMongoCollection(); // cursor $cursor = $this->getCursor(); $batchLimit = 100; $inProgress = true; // copy data while ($inProgress) { // get next pack of documents $documentList = array(); for ($i = 0; $i < $batchLimit; $i++) { if (!$cursor->valid()) { $inProgress = false; if ($documentList) { // still need batch insert break; } else { // no documents to insert - just exit break 2; } } $documentList[] = $cursor->current(); $cursor->next(); } // insert $result = $targetMongoCollection->batchInsert($documentList); // check result if (is_array($result)) { if ($result['ok'] != 1) { throw new Exception('Batch insert error: ' . $result['err']); } } elseif (!$result) { throw new Exception('Batch insert error'); } } return $this; }
/** * @expectedException \Sokil\Mongo\Document\OptimisticLockFailureException */ public function testOptimisticLock() { // init connection $client = new Client('mongodb://127.0.0.1'); $client->map(array('test' => array('phpmongo_test_collection' => array('lock' => Definition::LOCK_OPTIMISTIC)))); // get collection $collection = $client->getDatabase('test')->getCollection('phpmongo_test_collection'); // create document $document = $collection->createDocument(array('param' => 'value'))->save(); // check version field set $this->assertEquals(null, $document->get('__version__')); // first read of document $doc1 = $collection->getDocumentDirectly($document->getId()); // second read of document $doc2 = $collection->getDocumentDirectly($document->getId()); // update first document $doc1->set('param', 'valueOfDoc1')->save(); $this->assertEquals(1, $doc1->get('__version__')); $this->assertEquals(null, $doc2->get('__version__')); // try to update second document $doc2->set('param', 'valueOfDoc2')->save(); }
public function setUp() { $this->client = new Client(); $this->collection = $this->client->getDatabase('test')->getCollection('phpmongo_test_collection'); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Sokil\Mongo\Client; $client = new Client('mongodb://127.0.0.1'); $client->map(['tz' => ['products' => '\\Models\\ProductsCollection']]); $database = $client->getDatabase('tz'); $products = $database->getCollection('products');
public function setUp() { $this->client = new Client(getenv('PHPMONGO_DSN') ? getenv('PHPMONGO_DSN') : null); $this->collection = $this->client->getDatabase('test')->getCollection('phpmongo_test_collection'); }
/** * * @param string $name * @return \Sokil\Mongo\Database */ protected function getDatabase($name = null) { return $this->client->getDatabase($name); }