Phalcon - Object Document Mapper


Advertisements


Before starting with the concepts of Object Relational Mapper (ORM) and Object Document Mapper (ODM), it is important to understand the difference between SQL and NoSQL databases.

The following table highlights the differences between SQL and NoSQL −

SQL NoSQL
They are also termed as Relational Databases (RDBMS) They are called as non-relational or distributed database
The structure of database is constituted as tables and views It consists of document based and graph databases
It includes a predefined schema It has a dynamic schema
It is very powerful for defining and manipulating data It is powerful in maintaining data as collection of documents

Phalcon has the ability to map with SQL and NoSQL databases. This is achieved with the help of Object Document Mapper (ODM) for NoSQL database and Object Relational Mapper (ORM) for SQL database.

In Phalcon, ORM concept comprises of creating a model associated with the given table-name as we have seen in the previous chapters. It follows all the referential integrity constraints.

Object Document Mapper (ODM)

It is an object associated with NoSQL database. As the name suggests it maps the document related module. Phalcon uses it to map with databases like MongoDB.

Example

Step 1 − Create a database of MongoDB named “test”. We will use this database to map with and get the appropriate response.

Test

Mongo

Step 2 − Check for the inserted records in the database. The command associated with it is −

db.collection.find() 

Inserted Records

It is observed that every document is mapped with ObjectId which is a feature of ODM. The value of ObjectId is unique and later used to fetch all the data stored with respect to that particular Id.

Step 3 − Set up model for the database created. A model is a class which extends Phalcon\Mvc\Collection. Test.php model will include the following code.

<?php 
use Phalcon\Mvc\Collection;  

class Test extends Collection { 
   public function initialize() { 
      $this->setSource("test"); 
   } 
} 

Step 4 − Configure the project including database connectivity in services.php.

// Simple database connection to localhost 

$di->set( 
   "mongo", 
   function () { 
      $mongo = new MongoClient();  
      return $mongo->selectDB("test"); 
   }, 
   true 
); 

// Connecting to a domain socket, falling back to localhost connection 

$di->set( 
   "mongo", 
   function () { 
      $mongo = new MongoClient( 
         "mongodb:///tmp/mongodb-27017.sock,localhost:27017" 
      );  
      return $mongo->selectDB("test"); 
   }, 
   true 
);

Step 5 − Print the values with respect to ObjectId with the help of TestController.php.

<?php  

use Phalcon\Mvc\Controller;  

class TestController extends Controller { 
   public function index() { 
      // Find record with _id = "5087358f2d42b8c3d15ec4e2" 
      $test = Test::findById("5819ab6cfce9c70ac6087821"); 
      echo $test->data; 
   } 
} 

The output will display data which matches the objectId. If the objectId is not matched as per the records in the documents, then the appropriate output will not be displayed as the number of records is fetched.

Data Display

Advertisements