Monthly Archives: June 2016

What is an ORM (Object Relational Mapping) ?

object-relational-mapping

Many of you heard a term ORM or Object Relational Mapping and thought that What is an ORM (Object Relational Mapping) ? ORM is a short form of Object Relational Mapping. ORM is a concept which encapsulate relational database schema and provide a object oriented interface to developers. ORM framework as are developed using an object oriented programming language like PHP, Java etc. ORM allow developers to query and manipulate the data from a relational database using an object oriented paradigm. ORM wrap around the relational database system (RDBMS) and create a virtual object oriented database that is used within and from the programming language.
Basically ORM define a mapping between your classes and database tables or class members and table columns and the ORM manage the communication between your codes and underlying database. ORM framework wrap relation database tables into the classes of programming language, so that instead of writing SQL queries to manipulate database, we can use methods and objects.

orm

Now question is that Why i use ORM frameworks? What are advantages of ORM ? So, here is your answer

Advantages of using ORM frameworks

  1. ORM framework protect you application against the SQL injection attacks. Hence the ORM frameworks filter and sanitize all data. So, you don’t need to worry about this.
  2. Since ORM handle all database related things like writing SQL statements, data type conversions etc. SO, it become easy in future if you want to change the database server of your application. Like your application currently use MySql and you need to use PostgreSQL as database. It become very easy and need very less efforts if you use an ORM. By the way in my six year of experience i seen this case only once.
  3. If you have more then 10 tables and more then 3 developers are working on an application. Then it is helpful to use ORM framework. Because ORM framework create a consistent code style for your application. So, it becomes easy to work and probability of meshing up become less.
  4. There are lots of stuffs are done automatically for you by ORM frameworks. For example database handling, table relations & joins, filtering data, I18N etc.
  5. It make the maintenance work easy for you. Because ORM framework follow DRY practice. So, you have to write your data models only at one place. So, it become easy to maintain and update codes.
  6. A good ORM take care of synchronisation of the data types between the programming language (Ex. PHP, Java) and the SQL database. So, it also handle the data type conversion related things for you.
  7. ORM supports to ACID properties and transactions (commit and rollback).

If i am not wrong now you are thinking that Does there are some drawbacks of using ORM framework. So, here is your answer

Disadvantages of using ORM frameworks

  1. You have to learn the ORM framework first. There is a learning curve in understanding and using ORM framework.
  2. ORM makes working easier. But unfortunately developers skip learning SQL and database related things.
  3. Performance is fine for normal queries. But but you can always do better with your own SQL queries.
  4. Some time ORM fails to compete against native SQL queries incase of complex queries.

What is HHVM (HipHop Virtual Machine)

hhvm

Many of php developers heard about HHVM and tried to understand What is HHVM (HipHop Virtual Machine) ? In this article i am going to discus some basics about HHVM.

HHVM or HipHop Virtual Machine has been developed by Facebook. HHVM initally released on December 9, 2011 and the latest stable released on March 30, 2016. HHVM supports Hack, PHP 5 and PHP 7.

HHVM or HipHop Virtual Machine is an open-source virtual machine based on the concept of just-in-time (JIT) compliler which is designed for executing programs written in Hack and PHP. By using the just-in-time (JIT) compilation approach HHVM achieve superior performance and also maintain the development flexibility provided by PHP. HHVM execute the code 9 times faster than usual.
HHVM compiles PHP into an intermediate high-level bytecode (HHBC). HHBC is a bytecode format specifically created for HHVM. This HHBC bytecode is then translated into x64 machine code at runtime by a JIT compiler. At this point HHVM also performs some additional optimizations through a dynamic analysis of the translated This compilation process allows for all kind of optimizations which cannot be made in a statically compiled binary code, thus enabling higher performance of Hack and PHP codes.

Advantages of HHVM

  1. HHVM is faster than PHP.
  2. High perform with low usage of resources.
  3. Compile to HHBC a more effective native code.
  4. It recompile only file in which changes were made.
  5. Is is compaitable to almost open source CMS, E-Commerce and Frameworks like CodeIgniter, CakePHP, Laravel, Magento, Opencart etc.

Disadvantages of HHVM

  1. Compile time a little bit high.
  2. Facebook could not povide support for HHVM. Which slow down its growth.
  3. Support is not avilable for HHVM.

For more info read here

How to install HipHop Virtual Machine (HHVM) on Ubuntu ?

Its very easy to install the HHVM on Ubutnu. So lets start .

1) First we need to import HHVM repository’s GnuPG public keys our apt-key. For this use below command

2) Now add HHVM’s repository to our list. For this use below command

3) Now update your apt-get. For this use below command

4) Now install HHVM using below command

This command will install HHVM on your machine. You can check the HHVM installation by using below command

This command will show you output like

install-hhvm-on-ubuntu

How to install swift on ubuntu

install-swift-programming-on ubuntu

I hope that you heard about the Swift Programming Language. Here we will discuss  that how to Getting started with Swift Programming on Ubuntu and How to install swift on ubuntu. If not the first question coming in you mind is “What is Swift ?“.

What is Swift ?

Recently Apple released their latest programming language, Swift. Swift is open Source. So, now anyone can use this on any system. Apple has made it easy to install and start Swift programming on Ubuntu. At this time we can install and run Swift for Ubuntu 14.04 or 15.10 (64-bit).

Swift is Apple’s newest programming language. It’s like an scripting language. Apple intended to replace Objective-C with Swift and make Swift the primary language for developing Mac OS X and IOS applications. Swift is very easy to learn and clean. It has a similar syntax to other popular programming languages.

So, now the next question is How to Install Swift on Ubuntu ?

To install Swift on Ubuntu follow this steps :-

  1. Open your terminal and login as root user for login as root you can use this command

    this will ask for your password. Just enter your password and done
  2. Now we need to install required dependencies for Swift. For this run this command
  3. Now download the swift for this use this command

    To install the latest version of Swift, visit the swift download page here and get the URL from there and use with this command. You just need to replace the path of tar.gz file.
  4. Now extract the file and move it to the /opt directory. For this run this command

    This will extract the files to /opt/swift/swift-2.2.1-RELEASE-ubuntu14.04
  5. Now we need to add Swift tool chain to our path. So we able to execute swift command from terminal. For this open your ~/.bashrc . To open file you this command

    Now add below code into ~/.bashrc file.

    You cam can now execute swift command to use the REPL or build Swift projects.

How to install Mongo DB on Ubuntu 16.04

MongoDB is a cross-platform, document oriented, No Sql database. MongoDB provides high performance, high availability, and easily scalable. MongoDB works on concept of collection and document. The official Ubuntu 16.04 MongoDB packages have not been published till now by the MongoDB maintainers. In this article i am going to explains you that “How to install MongoDB on Ubuntu 16.04

To install MongoDB on your Ubuntu 16.04 follow this steps :-

  1. First login with root user. For this press CTRL + ALT + T or open terminal and type this command

    This will prompt you for your password, enter your password and press enter key.
  2. Now we need to add MongoDB repository on your Ubuntu 16.04 package repositories. By default MongoDB is already comes in Ubuntu package repositories but to install the latest version of MongoDB to Ubuntu 16.04 installation we add the official MongoDB repository to our Ubuntu 16.04 installation. To add this run this command

    Once you run this command you will see an output like

    step-1-how-to-install-mongodb-on-ubuntu-16.04

     

    To install any package or software in Ubuntu we use apt command. So now we have to MongoDB repository details so apt can know from where it need to download the MongoDB Packages. For this use this command


    Now we need to update Ubuntu 16.04 package list. For this use this command.
  3. So now we are ready install the MongoDB on our machine. For this use this command

    This command will install MongoDB also some helpful tools for  management of the MongoDB. Now we need to add MongoDB as a service on Ubuntu 16.04. For this we need to create a unit file which will describe MongoDB as service to Ubuntu 16.04. So we can start or stop the MongoDB service. So now we need to create a file named mongodb.service under the /etc/systemd/system directory.  For this use this command

    This command will create the file and open for editing. Now paste the below codes into this file

    Now just start the MongoDB Service. For this use this command

    This command will start the MongoDB. Hence, this command does not return any output. So, if need you can check service status using this command

    In case you want to stop the MongoDB service use this command

    In case you need to restart the  MongoDB service

    So, it done, We successfully installed the MongoDB on Ubuntu 16.04.

How to extract text from the PDF file and save to database using PHP

extract-text-from-pdf-using-php

Before some time my friend ask me that How to extract PDF content using  PHP ? as he want parse PDF files uploaded by users on a website and save the details to database.  To fulfill this requirement after a time i used this PHP class to extract text from the PDF file . This php class extract the text content from PDF files’s content as text. Once we got the data in the text format we can manipulate that data using Regular Expressions and  save into our database.

 

Install and Configure Nginx on UBUNTU 14.04

Nginx is an Web Server that is better than apache on various parameters . Ngix can be used as web server or reverse proxy server. Today at this time Nginx is very much popular in web server  and used for hosting very large and high traffic web applications.

Now install Nginx :-

  • Update the local package index using
  • Now install Nginx using this command
  • So, the above command installed the nginx at your server. Now to check this open your browser and put
    http://localhost/ or if you installed on your vps than you can access it using your public IP. To find public IP address you can use this command

    you will see the default welcome page of the Nginx

  • You can see nginx config
    /etc/nginx/sites-available/default
  •  Your Nginx root directory location is /usr/share/nginx/html.

Generate a PDF in CodeIgniter using MPDF

Many times we need to generate reports in our applications it may be in the form of charts, excel sheets, or PDF. So, now i going to discuss How to generate a PDF in CodeIgniter ?

I assume that you are at an intermediate level and able to generate the report in HTML form and now only facing problem to convert it into PDF format.

Here we are going to Generate a report in PDF format. There are many libraries available for these purpose like FPDF, PDF etc. In our case i used MPDF and CodeIgniter version 2.1.2 .  you can download MPDF from here .

Now, you need to extract the MPDF and put all the files at ./application/helpers/mpdf.  We have to create a custom helper. Here i create a helper file as /application/helpers/My_Pdf_helper.php. The code will be as follows :

Now You need to use this helper into your controller and use into function. And the function will be look like as follows :-

This function will directly download the pdf file.

An Introduction to Websockets

This days a great evolution comes into web development. Many new technologies get introduces and web applications become better. Before few year each time we need to refresh web pages to get data after that Ajax came into light and solve this issue. Ajax became very usefull and using widely this days. But ajax itself has some limitations and suitable for half-duplex communications (one way communication). Suppose we to show notifications alert to user in that case we need to send an ajax request for a particular time interval to check if any notifications are there. This make unnecessary hits on server, consume bandwidth as well as increase unnecessary load on server. To overcome this problem websockets get introduced.

Websocket is protocol providng a full duplex communication over a TCP connection. While HTTP ot HTTPS was a protocole that supports half duplex communication and ajax use HTTP. Websocket create a persistent connection between client and server. So, anyone of them can send and receive data anytime.

A secure version of the WebSocket protocol is implemented in :-

  • Firefox 6 and above (Was disabled in 4 and 5 due to security),
  • Safari 6 and above
  • Google Chrome 14 and above
  • Opera 12.10 (Was disabled in 11 due to security)
  • IE 10.4

Server Side Implementation library for Websockets :-

  • C++ : libwebsockets
  • Node.JS: ws, socket.io
  • Java: Jetty
  • PHP: Ratchet, phpws
  • Python: Tornado, pywebsocket
  • Ruby: em-websocket

How to use multiple database server with CodeIgniter ?

Use multiple database server with CodeIgniter 3  or CodeIgniter 2

Before some time i got a chance to work on an interesting project. That was developed using Codeigniter as framework. Now, the challenge is that we need to use two differ database server as well as different RDBMS server. One is Mysql and other one is Postgresql. It was really simple to use two database. This is the way I did this for two database same can be used for multiple databases.
So here i am going to show you how you can achieve this. First set your db configuration for both db into config/Database.php. First define your default db settings. In my case default is MySql.

Now define your second db settings. In my case this one is PgSql.

Database configuration is done. Now you can can connect to your database by using.

This will connect to your default DB. You need to manually connect to postgredb. Using this.

Hence we want to connect to multiple database simultaneously. We need to do in following way

And instead of using

We need to use in this manner

Also you can create your on Model Class by extending CI_Model and handle this connection related stuff into that. To do this create a file named application/code/MY_Model.php
Code of this file will be look like

For more detail see here