Zend, MongoDB, Doctrine a perfect relationship !

In a previous post I have discussed about CodeIgniter framework and how to build a simple app using MySQL with no ORM support. Zend is also a good PHP MVC framework which is most popular nowadays, after the Zend company introduced it newer version Zend 2. It has more robust and developer friendly enhanced features included such as routing, form creation, additional module support(3rd party) and many more.

The NoSQL db has become a first selection nowadays, since the traditional server-side bulk database handling is getting frustrated sometimes ,but it is easy to deal with NoSQL because it has on-built robust and fast data execution feasibility, no need of very complex and disgusting queries to execute, and most suited for bulk data handling. Basically the best NoSQL giants are

1. MongoDB
2. Redis
3. Apache CouchDB

Those has its own and unique features , such as Redis not suitable for bulk data handling but suitable for real time communication web apps.
But as a whole , all NoSQL systems primarily use JSON stuff for internal and external data processing. As an example for data insertions and retrieves they use simple JSON queries.

Here we discuss about MongoDB as a NoSQL engine for our Zend application.

So what is Doctine ? sometimes word makes you weird but it is an excellent PHP library for Object Relational Mapping (ORM) tasks. As we know Java use Hibernate and .NET use LINQ or NHibernate as their ORM tool, PHP not going to become second in ORM world, yes, it has Doctine!

So, the time to discuss about a basic application using Zend,MongoDB and Doctrine, are you ready?

First we discussed about configuration part of all those three units(The installation and configuration)

Get Zend Framework .

Basically, as a practice we use Zend Skeleton application to build a new application other than building Zend application from sketch.Its pretty easy , if you have git installed on your machine just clone it to your machine using this command

git clone https://github.com/zendframework/ZendSkeletonApplication.git

After clone it copy it to desired location(usually in Linux /var/www/ ,Aka-Document Root)
(I think you already set up PHP+MySQL+Apache on your system, if not do it now, just in Ubuntu/Debian/Mint apt-get install php mysql-server apache2 ……)
Then you need to install Zend inside this skeleton application, what you have to do is , go to your skelton application location using your terminal or cmd. You can see there are two files called composer.phar and composer.json is available in that directory.The composer.json includes the libraries/modules you need to download (simple dependency list) to the for application up and running. You may see Zend is already in your composer.json file.
Just execute the command

composer phar self-update


composer phar install

If you like you can add a separate virtual host for your application, or other wise just develop it from your document root.For development purposes I recommend you to use NetBeans-PHP IDE to increase your development speed and for self understanding the structure and flow of the code.
Other than that you may need to mod_rewrite on in your Apache configuration.

If you are done, go to http://localhost/ZendSkeletonApplication to visit the pre-built Zend2 skeleton application.

Get Mongo .

Download it from http://www.mongodb.org/downloads and install or the easy way for Debian like systems is

apt-get install mongodb

Don’t forget to install mongo-php library because missing this thing is put you in a deep trouble,

pecl install mongo

Also you need to add line extension=mongo.so in your php.ini file (may be found it in two places just add that line in both, just search to find them).

Check whether your Mongo library is bound with your PHP installation, (echo phpinfo();)

So it is Doctine time .

In order to install Doctrine module to your skeleton application you need to add Doctrine dependencies in your composer.json file , open it and add following line under require section,

"doctrine/doctrine-mongo-odm-module": "dev-master"

and change minimum-stability as,

"minimum-stability": "dev",

Then run

composer phar update

This will install Doctrine module under vendor section in your skeleton application (Zend library also goes to vender section)

So if no errors up to now? you are almost done, then we discuss how to develop an application in next article.

Thank you,

Useful Links :






The Trend Now, NodeJS (Server side javascripting)

You may know several server-side traditional and popular languages like PHP, JSP, and ASP or RUBY or whatever, but what about client-side? yes, you need to know JavaScript, right? But .. what about if we can rely on one language for both server-side and client-side, yes we can manage both aspects with one solution, that is NodeJS. NodeJS which was written using C++, which is a kind of engine and it can compile JavaScript code and execute in a real-time manner , yes asynchronously! This aspect is very useful if you need some real-time web apps such as chat applications to build. With NodeJS its very easy than you think.

So how to begin with NodeJS …

First, you need to install nodejs on your server/pc machine, its pretty simple, if you are developing under Debian/Ubuntu environment just apt-get install nodejs ,thats it, or if you need binary stuff, those are available in http://nodejs.org/download/ site.

if you are willing to install nodejs from your os package manager just execute following with super user privilages


#apt-get install nodejs


#yum install nodejs

But the better way is download the tarball pakage and extract in a proper location, as an example

$tar xvzf node-v0.8.21-linux-x86.tar.gz
$sudo cp -R node-v0.8.21-linux-x86 /opt/

For your easiness , we can create a symbolic link under /usr/bin/ directory then no need to worry about executing node/npm
(this step is optional, you already can access your node using /opt/node-v0.8.21-linux-x86/bin/node command)

$sudo ln -s /opt/node-v0.8.21-linux-x86/bin/node /usr/bin/node
$sudo ln -s /opt/node-v0.8.21-linux-x86/bin/npm /usr/bin/npm

Now you can access your latest node in console!

So, what is npm?

npm is the package manager for nodejs, it contain large number of useful packages to make your work done easily. As an example you no need to
code socket programming from scratch,node package manager gives you socket.io package so you can download it and you can use it in your app.
So there are large number of useful and frequently needed packages there.

Express – easy MVC web framework for node
Jade – NodeJS web UI engine
Socket.io – Socket Programming
Redis – Redis DB
Mongoose – Mongo DB etc…

Create a simple app

first create a file called app.js in a convenient location(In my case it is C:\Users\Duminda\NodeJSEx or ~/NodeJSEx)
copy following code in your app.js

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Hello from NodeJS");

console.log(“Node Server running on port 3000”);

execute your file using

$node app.js 

This is a simple web content display on http://localhost:3000 using nodejs



Create a simple app with a node package

Here, we create a simple app with colors module
Colors module helps you to dispay console logs with colors

first change your code to follows

var http = require('http');
var colors = require('colors');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Hello from NodeJS");

console.log(“Node Server running on port 3000”.yellow);

here we use colors pakage then we need to install it

$npm install colors

it will download the colors pakage from https://registry.npmjs.org/ and available under /your/project/path/node_modules directory
then execute this

$node app.js

you can see yellow colored output in the console !

If you want to install some pakage to accesss globally use npm -g install colors

If you have multiple node package dependancies in your project you not need to install all of them one by one
just create a file called packages.json and declare all your dependancies there

"name": "NodeOCR",
"version": "0.0.1",
"dependencies" : {
"nodecr" : "*",
"mysql" : "*",
"express" : "*",
"jade" : "*"
"engine": {
"node": ">=0.6"

then you can install all dependancies by executing

$npm install

thats all !

In a next turorial we will disscuss how to make a simple Express Web Application, with MongoDB
See you soon !