protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Welcome to the setup of lxHive!</info>'); $helper = $this->getHelper('question'); $question = new Question('Enter a name for this lxHive instance: ', 'Untitled'); $name = $helper->ask($input, $output, $question); $connectionSuccesful = false; while (!$connectionSuccesful) { $question = new Question('Enter the URI of your MongoDB installation (default: "mongodb://127.0.0.1"): ', 'mongodb://127.0.0.1'); $mongoHostname = $helper->ask($input, $output, $question); $client = new Client($mongoHostname); try { $mongoVersion = $client->getDbVersion(); $output->writeln('Connection succesful, MongoDB version ' . $mongoVersion . '.'); $connectionSuccesful = true; } catch (\MongoConnectionException $e) { $output->writeln('Connection unsuccesful, please try again.'); } } $question = new Question('Enter the name of your MongoDB database (default: "lxHive"): ', 'lxHive'); $mongoDatabase = $helper->ask($input, $output, $question); $currentConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Config/Config.template.yml')); $mergingArray = ['name' => $name, 'database' => ['host_uri' => $mongoHostname, 'db_name' => $mongoDatabase]]; $newConfig = array_merge($currentConfig, $mergingArray); $yamlData = Yaml::dump($newConfig); file_put_contents(__DIR__ . '/../Config/Config.yml', $yamlData); $output->writeln('<info>DB setup complete!</info>'); }
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 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'); }
/** * Get mongo client instance. * @return array $config */ public static function getClient() { // get configurations from config file here. $config = file_get_contents(__DIR__ . '/../config.json'); $config = (array) json_decode($config); $client = new Client($config['dns'] . '/' . $config['database']); $client->useDatabase($config['database']); return $client; }
public function __construct(Client $client, $database) { $this->client = $client; if ($database instanceof \MongoDB) { $this->mongoDB = $database; } else { $this->mongoDB = $this->client->getMongoClient()->selectDB($database); } }
/** * * @return \MongoDB */ public function getMongoDB() { if (empty($this->database)) { $this->database = $this->client->getMongoClient()->selectDB($this->databaseName); } return $this->database; }
/** * @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(); }
/** * 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; }
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'); }
public function testSetMajorityWriteConcern() { $this->client->setMajorityWriteConcern(13000); $this->assertEquals(array('w' => 'majority', 'wtimeout' => 13000), $this->client->getWriteConcern()); }
/** * * @param string $name * @return \Sokil\Mongo\Collection */ protected function getCollection($name) { return $this->client->getCollection($name); }
}); if (PHP_SAPI !== 'cli') { $app->url = Url::createFromServer($_SERVER); } // Error handling $app->error(function (\Exception $e) { $code = $e->getCode(); if ($code < 100) { $code = 500; } Resource::error($code, $e->getMessage()); }); // Database layer setup $app->hook('slim.before', function () use($app) { $app->container->singleton('mongo', function () use($app) { $client = new Client($app->config('database')['host_uri']); $client->map([$app->config('database')['db_name'] => '\\API\\Collection']); $client->useDatabase($app->config('database')['db_name']); return $client; }); }); // CORS compatibility layer (Internet Explorer) $app->hook('slim.before.router', function () use($app) { if ($app->request->isPost() && $app->request->get('method')) { $method = $app->request->get('method'); $app->environment()['REQUEST_METHOD'] = strtoupper($method); mb_parse_str($app->request->getBody(), $postData); $parameters = new Set($postData); if ($parameters->has('content')) { $content = $parameters->get('content'); $app->environment()['slim.input'] = $content;