Tutorials: Working with NoSQL Databases

Setting NoSQL Databases

In order to be able to query SQL databases (eg: MySQL), you first need to open bootstrap index.php file and add this event listener:

$object->addEventListener(::APPLICATION, "NoSQLDataSourceInjector");

then create in stdout.xml a tag (which will hold your connection settings), child of DEVELOPMENT ENVIRONMENT tag (because credentials will be different on another environment), child of tag. Example:

<local> <server driver="redis" host="localhost"/> </local> </sql>

If your site uses multiple database servers for a single DEVELOPMENT ENVIRONMENT, it is allowed to have multiple entries there. To learn more how to configure this tag and drivers supported, check official documentation!

Querying NoSQL Databases

Querying NoSQL databases requires you completed setting NoSQL databases section!

Retrieving a Connection

If you have a single server per DEVELOPMENT ENVIRONMENT, to get a connection, use:

$connection = ::getInstance();

If you are having multiple servers per DEVELOPMENT ENVIRONMENT, use this instead

$connection = ::getInstance(SERVER_NAME);

Where SERVER_NAME is value of "name" attribute at matching tag.

Both will return a single per session, reused until script ends and automatically closed afterwards (unless driver is apc/apcu, requiring no connections).

Running Statements

Once a connection is retrieved, you can run basic document database logic such as:

$connection->set(KEY, VALUE, EXPIRATION);

Example:

// saves a key named "foo" with value "bar" in DB that expires in 10 seconds $connection->set("foo", "bar", 10); // retrieves value of "foo" from DB $x = $connection->get("foo");

This works the same way as PDO, hiding complexities of driver used. If, however, basic operations are not enough and you need to work with actual vendor logic use:

$vendorDriver = $connection->getDriver();

then use vendor official documentation (eg: redis) to query that object.

×