- Phalcon - Security Features
- Phalcon - Object Document Mapper
- Phalcon - Working with Forms
- Phalcon - Asset Management
- Phalcon - Multi-Lingual Support
- Phalcon - Session Management
- Phalcon - Cookie Management
- Phalcon - Database Migration
- Phalcon - Query Language
- Phalcon - Scaffolding Application
- Phalcon - Switching Databases
- Phalcon - Database Connectivity
- Phalcon - Routing
- Phalcon - Views
- Phalcon - Models
- Phalcon - Controllers
- Phalcon - Configuration
- Phalcon - Functionality
- Phalcon - Application Structure
- Phalcon - Environmental Setup
- Phalcon - Overview
- Phalcon - Home
Phalcon Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Phalcon - Object Document Mapper
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 highpghts 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 abipty 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 pke MongoDB.
Example
Step 1 − Create a database of MongoDB named “test”. We will use this database to map with and get the appropriate response.
Step 2 − Check for the inserted records in the database. The command associated with it is −
db.collection.find()
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 PhalconMvcCollection. Test.php model will include the following code.
<?php use PhalconMvcCollection; class Test extends Collection { pubpc function initiapze() { $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 MongoCpent(); return $mongo->selectDB("test"); }, true ); // Connecting to a domain socket, falpng back to localhost connection $di->set( "mongo", function () { $mongo = new MongoCpent( "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 PhalconMvcController; class TestController extends Controller { pubpc 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.
Advertisements