RussellHarrower RussellHarrower - 1 year ago 66
Smarty Question

Which mongodb Drive should I now be using?

Ok so clearly I have been living under a rock or something, because I must be the only one who is still using an outdata driver for connecting PHP to mongodb
I know get this message when I visit that url

This extension is deprecated. Instead, the MongoDB extension should be

Now I know I can still use it, and most things will work, but to make sure we stay with the official drivers and up todate drivers.

I need some advice, the thing I am not understanding or maybe I am, either way it seems like the new driver means I will have to imploment a 3rd party plugin better know as Libraries and Frameworks.

However I would rather not use those ones listed there as I can't seem to find one for smarty-tpl framework, and my other clients all use to use

static::$_db = new MongoClient

I don't know if I can use something like this still?

I really hope someone is able to help me with this question.

Clear cut questions - if above is to over the place

  1. Is their a plugin for smarty-tpl to connect to the new mongodb

  2. Is their a recommened framework or Libraries which are
    coded by Mongodb officially?

  3. Is their a
    new MongoClient

    connection string for the new driver, if so please point it out as I
    can't find it on mongodb documents which maybe it's i have 100
    things on at once but keeps taking me to old mongo dirver.

Furthering this question:

Let's say we go with option 3 in the list above is the correct function now

new MongoDB\Driver\Manager

note I have always used
new MongoClient
and to call the db I use

$siteDB = SITEDB;
return static::$_db->$siteDB;

So that is easy, but I am going to guess Manager is not the database or is it just a really long way of saying this is the client.

I want to not have to change to much as I have a function in my class which handles db connections

public static function db()
if (!static::$_db) {
static::$_db = new MongoClient("mongodb://".SITEDBUSERNAME.":".SITEDBPASS."@".SITEDBURL.":27017/".SITEDB);
$siteDB = SITEDB;
return static::$_db->$siteDB;

Answer Source

The two main differences between the old extension and the new one are:

  • the extension has been split into two, with the low-level driver written in C, and the user-friendly interface built on top in PHP; this allows easier maintenance and customisability
  • the PHP library has been rearranged a bit in line with modern practices, such as namespaces

Unless you're doing something unusual or really hate the default library, you don't need to care about the split between extension and library: just install both, and carry on using the user-friendly functions as before.

The manual page on using the PHP library includes this example:

require 'vendor/autoload.php'; // include Composer goodies

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;

$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );

echo "Inserted with Object ID '{$result->getInsertedId()}'";

So the only changes are going to be things like find MongoClient and replace with MongoDB\Client.

I'm not clear what you mean by Smarty integration (I wouldn't normally expect the data store and templating engine to interact directly), but if that's a third-party plugin, you might need to look for or write an updated version that puts \ in the right place etc.