-
Tell me please how to use mongodb? I installed phalcon, devtools, incubator 3.4, mongodb 4.4. Where can I insert settings (dbname, login, password), config.php? How should I use incubator adapter, services.php? Where can I insert php class Collection, which extends MongoCollection, in models? Or any other ways? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hello, Also use updated mongo-db composer package - https://github.com/phalcon/incubator-mongodb Code example: $di->setShared('mongo', function () {
$mongo = new Client("mongodb://mongo-db-url:27017");
return $mongo->selectDatabase("hello_world");
});
// Registering the mongoDB CollectionManager service
$di->setShared('collectionsManager', function () {
return new MongoDBCollectionManager();
}); Collection <?php
use Phalcon\Incubator\MongoDB\Mvc\Collection;
class UsersCollection extends Collection
{
public function onConstruct(): void
{
$this->setSource('users');
$this->useImplicitObjectIds(false);
}
} Controller <?php
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Model\Resultset;
class MongoController extends Controller
{
public function indexAction()
{
return UsersCollection::findFirst([
'conditions' => [
'_id' => mt_rand(1, 10000),
],
'projection' => [
//'id' => 0,
],
]);
} |
Beta Was this translation helpful? Give feedback.
-
You can see example here. |
Beta Was this translation helpful? Give feedback.
https://github.com/TechEmpower/FrameworkBenchmarks/blob/9a5b198b0b054fa068d3f9e6e0e9f9fb40a4b71a/frameworks/PHP/phalcon/public/index.php#L8
You can see example here.