Collection name -> array definition:
['acmeDatabaseName' => ['acmeCollectionName' => ['class' => '\Acme\Collection\SomeCollectionClass']]]
Collection name -> collection class name (deprecated: use definition array):
['acmeDatabaseName' => ['acmeCollectionName' => '\Acme\Collection\SomeCollectionClass']]
Collection's class namespace (deprecated: use definition array):
['acmeDatabaseName' => '\Acme\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); }
/** * @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(); }
<?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');
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; $parameters->remove('content');