Learn How To Use PHP With OrientDB Driver

OrientDB Driver


GraphDB is a new breed of databases that is perfect for Big Data Applications like Social Networks and Artificial Intelligence. GraphDB can store all types of data and can perfectly manage the relationship among this data regardless of its size.

In this article, we introduce the OrientDB Driver with PHP. OrientDB is a relatively new database in the GraphDB Marketplace. It is one of the most advanced GraphDB solutions available today. The OrientDB Community edition is free for any purpose and is distributed under Apache 2 license.

Unfortunately, covering OrientDB itself is out of the scope of this article, but you will find a ton of resources which already cover this in detail. For this article, we assume that you have OrientDB installed on your Computer and have a basic idea about what OrientDB is and how it works.

PHP Developers have two options to communicate with the OrientDB Database. First way is to use the REST API. We can make HTTP calls using PHP cURL library. The other approach, that is much cleaner is to use the Official OrientDB Driver for PHP.

This Driver is named PhpOrient and is available at: https://github.com/orientechnologies/PhpOrient.

Once you include this driver in your PHP code, you are good to start using code samples provided in this article.

Composer users can rather install the official OrientDB Driver from the Packagist Repository, using the following Composer command:

$ php composer.phar require "ostico/phporient:dev-master" --update-no-dev

The following section consists of performing basic tasks using OrientDB Driver from your PHP code. A basic understanding of OrientDB is required past this point. Also, please make sure your OrientDB Server is running before proceeding.

Connecting to the OrientDB Server
The OrientPHP Driver has a main class called PhpOrient . Inside our PHP code, we create an instance of this class and perform all our operations via this instance. Please have a look at the following code snippet which connects to the OrientDB Server:

There are several things to note here. First of all, the default port on which OrientDB Server listens is 2424 . If the connection is successful, the  connect()  method returns an integer and in case of any errors, a PHP Exception is thrown.

Listing all available databases in OrientDB
In the following code example, we will call the Instance Method called  dbList() that returns an associative array containing all available databases in our OrientDB Server. This function throws an exception in case of any error.

Creating an OrientDB database
Databases can be created by calling the single method in OrientDB. This method is dbCreate() . This method returns a Boolean TRUE on successful database creation and throws an Exception in case of any errors. The following code demonstrates the database creation process:

Deleting the OrientDB database
To delete the database in OrientDB, the Instance method  dbDrop()  is used. Please have a look at the following code snippet:

The  dbDrop()  method returns Boolean TRUE on successful deletion and throws an Exception for any errors that might occur.

OrientDB Database Existence Check
Often there is a need to dynamically check for the existence of database. This can be checked using the  dbExists()  method. This is demonstrated below:

The  above method returns Boolean TRUE if the database exists and Boolean FALSE otherwise.

Working with the databases in OrientDB
Unlike other RDBMS databases, OrientDB requires database to be opened after the connection has been established with the OrientDB Server to perform CRUD functions on its records. We call a method named  dbOpen()  to open our database. This is demonstrated below:

On success, the above method returns an Object of type ClustersMap . In case of any errors, an Exception is thrown. OrientDB supports SQL like syntax to work with the records.
So, we use SQL like syntax on non schematic data. This is the beauty of OrientDB, which brings best features of both SQL and NoSQL Worlds at our disposal.
All SQL commands are sent to the OrientDB server using the method command() . The following sections demonstrates the code in action.

Creating a Vertex Class in OrientDB
In OrientDB, records are represented as Classes. Vertexes and Edges have their respective classes i.e. V  and E . All our Vertexes need to inherit from the class V , similarly, all the Edges inherit from the class E . In the following code snippet, we create our Vertex Class named Person . We will then create our records for this class in the later sections.

The above code snippet will create a Vertex class named Person . This will return the ClustersMap object on success and throws an Exception in case of any issues.

Creating a Vertex Record in OrientDB
We can now create our Records of class Person. We create two records as follows:

The above code snippet creates two Vertex Records of class Person in our database. This data is represented in the JSON format in OrientDB.

Selecting all Records in OrientDB
Now to retrieve the above created records, we will run an SQL query using the  query() method. This method returns an array of Record Objects that consists of our data. This is demonstrated in the code snippet below:

Along with the actual data, the returned objects also consist of the position of the Record in the Filesystem (which is represented by the keys cluster and position ) and the version key. These keys are required to perform CRUD on the records.

Updating a Vertex Record in OrientDB
To update a record in OrientDB, we need to supply additional 3 fields along with the actual data. Firstly, it is the cluster and position of the record. Lastly, the version of the record, this has to be the latest version. These three fields are maintained for every record in OrientDB. The cluster and position always stays the same but the version gets auto incremented with every record updation. The following code snippet shows a very simple demonstration of the Record Updation process:

The Record object is returned on successful updation and an Exception is thrown in case of any errors.

Deleting a Vertex Record in OrientDB
Deleting a Record in OrientDB is a one liner. We need cluster and position fields of the Record which is to be deleted. Unlike the Updation process, we don’t need the version of the Record. Deletion process is demonstrated in the example below:

The above code returns Boolean TRUE on successful deletion and Boolean FALSE in case of any errors.

In this article, we covered the basics of using the OrientDB Driver with PHP. You should now be able to give a Hands On to the OrientDB Driver and try more Driver methods.


Please enter your comment!
Please enter your name here