Thursday, November 27, 2014

MongoDB Tutorials : Aggregation Framework



MongoDB Aggregation Framework groups set of documents together and performs certain operation on this grouped documents and returns results in the form of documents.
MongoDB aggregation answer to those query which requires grouping of documents.


Aggregation framework works on three type of model
  • Aggregation pipeline
  • Map Reduce
  • Single Purpose Aggregation Operations


Now let see how MongoDB Aggregation Framework works with simple example.

Suppose you are MongoDB application developer in a respected company and you have been given a MongoDB database that holds information about human population which is distributed according to cities and their states.

here is one sample document.


{
        "city" : "ACMAR",
        "loc" : [
                -86.51557,
                33.584132
        ],
        "pop" : 6055,
        "state" : "AL",
        "_id" : "35004"
}


for this tutorial collection name is zips.

on first day your manager come and asks you to calculate total population of state DC.

your simplest approach to solve above query would be find all documents which have DC as state. then iterate them one by one and add population to a variable.
here is the a sample script

MongoDB script 1

 var record = db.zips.find( { "state" : "DC"} )
 var sum = 0;
 record.forEach(function(rec){ sum = sum + rec.pop })
 print("Total population of State DC = " + sum)

Total population of State DC = 606900

This works fine and given task is done.
later, next day your manager comes and tells you to find total population of each states.

now this query is little tricky, but you would modify you first MongoDB script and make it is given below.

MongoDB script 2


 var states = db.zips.distinct("state")
 
 states.forEach(function(state){

   var records = db.zips.find({"state" : state});
   var sum = 0;

   records.forEach(function(record){
     sum = sum + record.pop;
   });

   print( state + " population: " + sum);
 });

yes, it will give you expected result. no doubt you are a good MongoDB developer.

but you have put extra efforts get result. it does not mean that you are not a good developer, rather you are not aware of MongoDB aggregation framework which produce expected result in one query.

following is aggregation query


db.zips.aggregate([ { "$group" : { "_id" : "$state", "population" : { "$sum" :
 "$pop" } }} ])
it will give you same result which in equivalent of second MongoDB script.
now let see how it works.

First look out the group clause of aggregate query.

db.zips.aggregate([ { "$group" : { "_id" : "$state" } } ])
 
 
it will group collections according to state



 
as documents are grouped now we can perform calculation on them like summation of population.


db.zips.aggregate([ { "$group" : { "_id" : "$state", "population" : { "$sum" :
 "$pop" } }} ])



hence you get expected result in one query using MongoDB Aggregation Framework.




Read More...

Sunday, November 2, 2014

PHP Tutorials : Autoload PHP Classes

This tutorials post will explain PHP autoloader and namespace concept using few basic question and answer regarding PHP autoload functionality along with some examples.

What is Autoloading in PHP?

PHP provides helper functions to include or import PHP files, if it has not been included before. if we use PHP autoload then we don't need to write require or include statement for each PHP class files.

Why we need auto loading?

It is a good question. why we need it, although we can write require or include statements whenever we need. As PHP has evolved using OOPs concept, we create more classes for our PHP project and create a separate PHP file for each single PHP class. Hence we increase the number of require or include statements. therefore we need some standard way to include each files rather specifying include statement by our self every time. that is why PHP has provided some useful methods to resolve this problem.

PHP Autoloading Methods

PHP uses two function, one is magic method __autoload and another is spl_autoload_register. each of these have their own benefit. they accept one parameter which turns into class name whenever we instantiate that class object. inside these method we could write our logic to load PHP class file.

What is difference between __autoload and sp_autload_resgister function?

__autoload method is magic method which means you can define it only once. therefore you would have only one logic to include PHP class files whereas spl_autoload_register is defined function which accept function name as parameter and we can define specified function with our logic. that mean we can have multiple implementation of inclusion of PHP files with help of spl_autoload_register.
e.g
<?php

/**
 * autoload method
 */
function __autoload($class)
{
  require $class.'.php';
}

//spl_autoload_register

/**
 * autoload method one
 */
function autoloadOne($class)
{
  require "/dir-one/".$class.".php";
}

/**
 * autoload method two
 */
function autoloadTwo($class)
{
  require "/dir-two/".$class.".php";
}

//register auto loader
spl_autoload_register("autoloadOne");
spl_autoload_register("autoloadTwo");

What is namespace?

PHP normally wont allow two class with same name. but later 5.3 introduce concept of namespace which create a ownership of class so that two different class can have same class name but with different ownership.

What is importance of namespace in autloading

when we autoload class we get a parameter into autload function. when you echo that parameter it will print that class name and namespace provides directory structure for classes.
e.g
<?php

namespace Module\User\Controller;
namespace Module\User\Entity;
namespace Module\User\Form;
namespace Module\User\Validator;

if we autoload above class then we will get whole namespace of that class along with class name e.g namespace_autoload.php
<?php

function __autoload($class)
{
  echo "class path = ".$class;
  die ;
}

new \Module\User\Controller();

// it will print
// class path = Module\User\Controller
with help of this information we can organize classes according to our project directory structure. let say you have following project structure

project
 |__Module
      |__User
          |__Controller
          |__Entity
          |__Form
 |
 |__autoload.php

then you can create PHP classes with namespace as



 project
 |__Module
     |__User
   |__Controller   \\ class : IndexController , namespace Module\User\Controller
          |__Entity   \\ class : User , namespace Module\User\Entity
          |__Form  \\ class : UserForm , namespace Module\User\Form
 |
 |__autoload.php
 
autoload.php
<?php

//include namespace

use Module\User\Controller\IndexController;
use Module\User\Entity\User;
use Module\User\Form\UserForm;

/**
 * Autload project files
 */
function __autoload($class)
{
  echo $class;
  require $class;
}

// instantiate classes
$controller = new IndexController();
//(it will print)  Module\User\Controller\IndexController and load this class

$user = new User();
//(it will print)  Module\User\Entity\User  and load this class

$userForm = new UserForm();
//(it will print) Module\User\Form\UserForm  and load this class
as you can see how namespace helps to organize project structure and autoload the classes.

What is difference between following two sets of statements

namespace autloading Direct include files

 use Module\User\Controller\IndexController;     require "Module\User\Controller\IndexController"; 
 use Module\User\Entity\User;       require "Module\User\Entity\User";
 use Module\User\Form\UserForm;           require "Module\User\Form\UserForm";

General difference is require statement accept full directory path where in case of namespace we can set logic to autoload PHP files with different directory structure and can minimize the namespace length. let see the example, suppose you have following directory for your project library files
project
 |
 |__vendor
 |
 |__lib
  |
  |__src
   |
   |__components
    |
    |__HTTP  //Request, Response classes
    |
    |__Controller  //BaseController classes
    |
    |__Form //BaseForm classes
 ..... many more files
using require statement need to write following code.
<?php

 require "/vendor/lib/src/components/HTTP/Request";
 require "/vendor/lib/src/components/HTTP/Response";
 require "/vendor/lib/src/components/Controller/BaseController";
 require "/vendor/lib/src/components/Form/BaseForm";
which is very lengthy and not properly organized with help of autoload and namespace we can reduce and class path and organize structure in much proper ways
<?php

define("LIB_PATH", "/vendor/lib/src/components");

use HTTP\Request;
use HTTP\Response;
use Controller\BaseController;
use Form\BaseForm;

/**
 *  autoload lib class files
 */
function __autoload($class)
{
  // set class path
  $class_path = LIB_PATH.$class;

  //include class file
  echo $class_path;
  require $class_path;
}

$request = new Request;
// (it will print)  /vendor/lib/src/components/HTTP/Request

$controller = new BaseController;
// (it will print)  /vendor/lib/src/components/Controller/BaseController

Summary


  • autoload helps to load all PHP classes of a PHP project.
  • namespace helps to organize your PHP classes
  • combination of autoload and namespace organize and load PHP project in standardized way.


Read More...

Saturday, October 25, 2014

JSON Tutorials : Getting Started


JSON is widely accepted text formatted structured data. JSON stands for "JavaScript Object Notation". In general JSON can represent 1. Object of database record. 2. Object to represent a list of HTML elements. 3. Result of search query. 4. Response of an Ajax call. Here you can see JSON is used in many different areas and for many different scenarios. This means it has simple data structure. most of programming languages adopt it and it can flow easily from one connection to another. You can find JSON office definition here JSON Official Site. JSON is represented by two structural types, which includes two primitive types. Structural types

Array: A sequential list of primitive data types between square brackets [ ]
Object: Collection of key, value pair stored inside curly braces { }, where value would be primitive data type

Primitive types:
There are two primitive types key and value. "key" should be string and "value (data type)" could be anything like integer, string , Boolean , empty or null. Lets see some example of JSON objects.

Array :

[
    "apple",
    "orange",
    "blackberry",
    "grape"
]
Object:

{
    "username": "zainabed",
    "first_name": "Zainul",
    "country": "India"
}


Mix Object:

{
    "username": "zainabed",
    "first_name": "Zainul",
    "country": "India",
    "hobby": [
        "reading",
        "programming",
        "sports"
    ]
}


JSON is widely accepted and used because
  • It is lightweight, that is why it is possible to transfer large set of data without exhausting internet bandwidth.
  • It is language independent, which means most of the programing languages have mechanism to accept or generate JSON objects, it doesn’t need extra functionality to make JSON objects compatible with other languages.
  • JSON can be as primary object for database system. it is used to store and read or perform CRUD operation on JSON which can be stored as documents. MongoDB is popular database system which based on JSON documents.
  • JSON can have Embedded documents as well, it helps to avoid expensive join operations on related documents. here rather using joins, one JSON documents can be embedded inside another.
  • JSON object can be used to represent HTML entities like list of user, tables or image gallery. AngularJs is most powerful tool to transform JSON to HTML.
  • JSON can also represent search result. search result could include search count, search title and description list , etc. all this can be included in a JSON object. Elasticsearch represent its search result in JSON object. as it is JSON object it can be used in any programming languages as result.
  • JSON play important role in drawing of charts. charts requires data set to create a particular chart and JSON object is perfect data model for it. D3 charts are always use JSON objects.
Frequently Asked Questions:

1: Can JSON starts with Array?
Answer: Yes, JSON can start with Array or Object, there is no such restriction.

2: What is the correct JSON content type for HTTP response?
Answer: Correct content type for JSON is application/json

3: Safely turning a JSON string into an object inside a JavaScript?
Answer: Following two function call will turn string to JSON object


jQuery.parseJSON( jsonString ); //Jquery

JSON.parse(jsonString); //plain JavaScript

Read More...

Thursday, October 23, 2014

Doctrine 2 Tutorials: Installation and Configuration



This tutorial post will help you to install Doctrine2 inside your PHP project and configure it so that you can use it's features in plain PHP project.

This post illustrates installation of Doctrine2 using Composer.

Installation

First create your project directory


 mkdir zainabed   

 cd zainabed

now create composer.json


vi composer.json

then add following repository information.


{
    "require": {
        "doctrine/orm": "*"
    }
}

Now you are ready to install it via composer, but first we need to install composer into you machine.


curl -sS https://getcomposer.org/installer | php

Then initiate following command to install Doctrine2


php composer.phar install

composer creates "autoload.php" file which helps you to autoload all PHP classes of Doctrine2 ORM project.

Configuration

First create configuration file configuration.php for Doctrine2 and include autoload.php inside it.


<?php

// configuration.php

// Include Composer Autoload 
require_once "vendor/autoload.php";

Then create database configuration details, like database name, username ,password.


// database configuration

$databaseParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'zainabed',
);

Then specify Entity path where you want to store all ORM Entities.


//entity path
$entityPath = array("src/Entity");

Now create Entity Manager object.
Entity Manager is center source of ORM which handle interaction between Entities and Database.


//annotation configuration
$config = Setup::createAnnotationMetadataConfiguration($entityPath, false);

//entity manager object
$entityManager = EntityManager::create($databaseParams, $config);

now you are ready to use Doctrine2 inside your PHP project.

following is complete configuration example which also includes configuration based on XML and YML.


<?php

// configuration.php

// Include Composer Autoload 
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

// database configuration
$databaseParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'zainabed',
);

//entity path
$entityPath = array("src/Entity");


//annotation configuration
$config = Setup::createAnnotationMetadataConfiguration($entityPath, false);

//entity manager object
$entityManager = EntityManager::create($databaseParams, $config);

//xml configuration
//$xmlEntituPath = array("/path/to/xml-mappings");
//$config = Setup::createXMLMetadataConfiguration($xmlEntituPath, false);
//$entityManager = EntityManager::create($databaseParams, $config);

//yml configuration
//$ymlEntityPath = array("/path/to/yml-mappings");
//$config = Setup::createYAMLMetadataConfiguration($ymlEntityPath, false);
//$entityManager = EntityManager::create($databaseParams, $config);

Doctrine 2 is now configured and ready to use.

Read More...

Thursday, October 16, 2014

Twig Tutorials: Install and Configure



In this tutorial we will see how to install Twig inside your PHP project and then configure it so that we can create use Twig template inside our PHP web application.

later we see a simple Twig template example which will display "Welcome to Twig template" message.

lets see each step one by one.

Install


we can install Twig template via Composer, Git or from PEAR.

in this tutorial post we will install Twig using Composer.

for that we need to create "composer.json" file.

{
    "require": {
        "twig/twig": "1.*"
    }
}


the from console run following command.

  php composer.phar install   

this command will install Twig library.


Configuration


To use Twig template we need configure it first, for that we will create index.php file which will setup Twig autoloader and generate the Twig environment.

using this Twig environment we will render Twig template

include __DIR__ . "/vendor/twig/twig/lib/Twig/Autoloader.php";

//register autoloader

Twig_Autoloader::register();

//loader for template files

$loader = new Twig_Loader_Filesystem('templates');

//twig instance

$twig = new Twig_Environment($loader, array('cache' => 'cache'));

//load template file

$template = $twig->loadTemplate('index.html');

//render a template

echo $template->render(array('title' => 'Welcome to Twig template'));


now we will create "template.html" file inside template folder.

<html>

  <head></head>

  <body>

    <h1>{{ title }}</h1>

  </body>

</hmtl>

and you will get following output when you run index.php file in localhost




That's it, we are done with Twig installation and configuration process.

Source Code

Download source code from this Github link
Read More...

Tuesday, October 14, 2014

Generate SSH Public Key for GitHub and Bitbucket

ssh key github bitbucket

This post demonstrate how to generate SSH private and public key with simple steps and help you to add public key inside your GitHub and Bitbucket account.

But first why we need this SSH public key?

Whenever you access or do some Git operation on remote repository using HTTPS connection from your local machine, you need to authenticate yourself first using username and password.

It looks simple earlier, but later becomes headache.

SSH connection helps us to authenticate using public and private keys, that we generate only once, later we don’t need to authenticate, SSH will do this behalf of us.

But how does it works?

SSH authenticate you using an identity. And this identity is combination of private, public key.

Private Key resides in your local machine and public key inside your GitHub or Bitbucket account.

When you access Git repository, SSH combines these two keys and authenticates you.
                                      
To use SSH you need to install it first. I assumed that you have already installed it, if not 
then please visit http://www.openssh.com and install SSH to your local machine.

Generate private-public keys

Us following steps to generate private and public key, later copy public key and add it in your GitHub and Bitbucket account.

Open terminal and type following command

  ssh-keygen            

It will ask location for key files, hit Enter to select default location

Then it will ask passphrase (password), you can type your password or cant keep it empty

It will ask re-passhprase

once done, keys get generated and get saved inside provided location. 
Here is sample output of ssh-keygen operation.

Then add your key to the ssh-agent.
  ssh-add ~/.ssh/id_rsa    

Later we need to add public key to GitHub and Bitbucket account. To do so we need get 
content of public key, this resides inside ~/.ssh path.

  cat ~/.ssh/id_rsa.pub     

Now copy this key and visit GitHub or Bitbucket to add it into your account.

GitHub

Open a browser and log into your GitHub account.

  • Select setting icon from top right corner of menu bar.
  • Then select “SSH keys” from left side panel
  • Now add new public key by selecting “Add SSH key” button. It will open a panel to enter public key.
  • Add label for public key and then add copied public key inside key area.
  • Now finish this process by clicking "Add key" button.


That’s it, you done with GitHub.

Bitbucket

Open a browser and log into your Bitbucket account.

  • Select Avatar->Manage Account from the application menu.
  • Under Security section Click SSH keys.
  • Then click Add key button. It will prompt a dialog to add public key.
  • Add label for public key and then add copied public key inside key area.
  • Now finish this process by clicking Add key button.


That’s it, you done with Bitbucket.
Read More...

Thursday, October 2, 2014

Symfony Tutorials: Event Dispatcher

Symfony Tutorials: Event Dispatcher

Symfony EventDispatcher is object which interacts with different set of objects when certain event happens.

To illustrate Event Dispatcher definition let’s consider the online shopping website example.

suppose you want to purchase a mobile from online shopping website , but unfortunately that mobile is out of stock.

Then you subscribe into online shopping website for this mobile availability.

When mobile comes in stock, online shopping website notifies you about mobile phone’s availability via email.

In above scenario


  • you are the Event Listener / Event Subscriber
  • mobile availability is Event
  • online shopping website is Event Dispatcher.



Symfony EventDispatcher works in same manner.for example, whenever there is HTTP request, Kernel creates a request object and it dispatches an event kernel.request.

Whoever subscribes to kernel.request event gets notified.

So here you might be having few questions in my mind.

What is Event?

Event object describe what event is and add some additional information so that its listener or subscriber can get enough information about event.

What is EventDispatcher?

EventDispatcher is central object which dispatches the event to all its listener or subscriber.
EventDispatcher maintains the list of all listeners of a particular Event.

use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher =new EventDispatcher();

What is Event Listener?

Listener is a object which performs a task whenever a associated event happens. 
But first we need to attach listener to Event Dispatcher for a particular event.

$listener=new MobileAvailabilityListener();
$dispatcher->addListener('store.mobile.available',array($listener,'sendEmailToUsers'));

Considering Online Shopping Store example let’s create a custom event and dispatch this event.

Here we want to create mobile availability event which get dispatched whenever mobile is available.

First we create static event class which holds event name and its instance information

Static Event Class


final class MobileEvents
{
/**
     * The store.mobile.available event is thrown each time when mobile 
  * is available in store
     *
     * The event listener receives an
     * Acme\StoreBundle\Event\MobileAvailableEvent instance.
     *
     * @var string
     */
const MOBILE_AVAILABLE='store.mobile.available';
}

Later, create actual event object.

Symfony uses Symfony\Component\EventDispatcher\Event class.

This class wont give us enough information about mobile availability therefor we need to subclass it and add additional information.

namespace Acme\StoreBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Acme\StoreBundle\Mobile;

class MobileAvailableEvent
{
  protected $mobile;

  public function__construct(Mobile $mobile)
  {
    $this->mobile=$mobile;
  }

  public function getMobile()
  {
    return $this->mobile;
  }
}


Later, create event listener, which sends emails to user regarding availability of the mobile phone.

use Acme\StoreBundle\Event\MobileAvailableEvent;

class MobileAvailabilityListener
{
  // ...

  public function sendEmailToUsers(MobileAvailableEvent $event)
  {
     // ... send email to users
  }
}



Now all is set, let’s attach this listener to the dispatcher. And dispatch the event.
use Symfony\Component\EventDispatcher\EventDispatcher;
use Acme\StoreBundle\Event\MobileAvailableEvent;
use Acme\StoreBundle\Event\MobileAvailabilityListener;
use Acme\StoreBundle\Entity\Mobile;
$dispatcher = new EventDispatcher(); // attach listener $listener = new MobileAvailabilityListener(); $dispatcher->addListener(MobileEvents::MOBILE_AVAILABLE, array($listener, 'sendEmailToUsers')); //if mobile is available then dispatch the event
$mobile = new Mobile();
$event = new MobileAvailableEvent($mobile);
$dispatcher->dispatch(MobileEvents::MOBILE_AVAILABLE, $event);


This is a every basic example, you can create event for user registration process and can send confirmation email via event listener or can do  many things using Symfony Event Dispatcher.
Read More...