Top PHP ORMs

| Page Views: 225

Top PHP Object Relational Mappers

In this listing we want to explore some of the top PHP ORMs in no particular order.

But first let's start by making some introductions.

What is PHP?

PHP is a general purpose programming language first created by Rasmus Lerdorf in 1994. Then it was called Personal Home Pages. However given the capability the language has acquired since then, PHP now stands for PHP: Hypertext Preprocessor in full. People use PHP especially for web development and some of your favorite websites and web applications like Wordpress,Facebook and Wikipedia run PHP. This atests it to its true power.

When making web applications you do need some form of database storage system. This a piece of software that can store our data securely. Then we can use various subroutines to connect and return that data. That database can be an RDBMS(Relational Database Management System) which commonly use SQL for manipulations or a NoSQL.

The more traditional is RDBMS and many systems use it. Hence Object Relational Mappers have been created to allow for higher abstractions over raw SQL statements when it comes to performing our CRUD(Create Read Update Delete) operations. ORMs are safer and easier and allow us manipulate databases via PHP rather than via SQL. They then convert our PHP statements into SQL.

Normally ORMs provide a minute performance as compared to SQL statements but this is in many apps not really noticeable. However they provide us with greater productivity.

Let's see some of the best PHP ORMs.

(a). ReadbeanPHP

ReadbeanPHP is one of the most framework-agnostic,popular and easiest to use ORM library for PHP. It is probably the easiest to use and is very reliable and readable. Readbean is uniquely a single file and you can simply drop it into your project and there you go. It is zero config and it will build your database schema for you.

RedBeanPHP requires PHP 5.3.4 or higher. RedBeanPHP is works on both ZEND PHP and HHVM.

Supported Database

RedBeanPHP supports all well known, open source, relational databases. These include: 1.MySQL 2.MariaDB 3.PostgreSQL 4.SQLite 5.CUBRID 6.Firebird/Interbase (experimental).

Snippets

You can create a given record or bean like this:

    $book = R::dispense( 'book' );
    $book->title = 'Learn to Program';
    $book->rating = 10;.

Then you can save or store it in database like this, returning an id:

$id = R::store( $book );

Then we can retrieve or select:

 $book = R::load( 'book', $id ); //reloads our book

Here's how you update the database:

    $book->title = 'Learn to fly';
    $book->rating = 'good';
    $book->published = '2015-02-15';
    R::store( $book );

And finally delete:

    R::trash( $book ); //for one bean
    R::trashAll( $books ); //for multiple beans

Learn more about Readbean CRUD here.

No. Location Link
1. RedbeanPHP View Full Documentation
2. GitHub View Repositoryl

(b). Propel ORM

There are three versions of Propel ORMs:

  1. Propel - Outdate and Unmaintained.
  2. Propel2 - High performance PHP ORM for PHP 5.5 and up.
  3. Propel3 - High performance data-mapper ORM with optional active-record traits for RAD and modern PHP 7.1+

Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.

Propel2 also relies on Composer to manage dependencies but you also can use ClassLoader (see the autoload.php.dist file for instance

Installation

Propel is available as a clone from the official Github repository, as a checkout from Subversion through Github and as a “traditional” tgz or zip package.

Supported Databases

Here are the database systems supported by Propel:

  1. MySQL,
  2. MS SQL Server
  3. PostgreSQL
  4. SQLite
  5. Oracle

Snippet Let's say you want to save/insert data:

<?php
/* initialize Propel, etc. */

$author = new Author();
$author->setFirstName('John');
$author->setLastName('Doe');
$author->save();

And if you want to read or select results:

<?php
$q = new AuthorQuery();
$firstAuthor = $q->findPK(1);

Here's how you update:

<?php
$author = AuthorQuery::create()->findOneByFirstName('Jane');
$author->setLastName('Austen');
$author->save();

Learn more about Propel CRUD here.

No. Location Link
1. PropelORM View Full Documentation
2. GitHub View Repositoryl

(c). Nextras ORM

We also have Nextras, a next generation ORM designed with ease and efficiency in mind. While it's not so much known, it also great especially if you like coding via Object Oriented paradigm. It has existed however for more than 4 years and still receives regular updates.

Here are some of it's great features in my opinion

  1. Clean Object Oriented coding.
  2. Regular updates. The current version is version 3.
  3. Proper easy to follow documentation.

Supported Databases

  1. MySQL
  2. Postgres
  3. MS SQL Server.

Here's a simple snippet:

$book = new Book();
$book->title = 'My Life on The Wall';
$book->author = $author;
$book->publisher = $publisher;

$orm->books->persistAndFlush($book);

And here's an example of fetching data:

$authors = $orm->authors->findAll();
foreach ($authors as $author) {
    echo $author->name;

    foreach ($author->books as $book) {
        echo $book->title;
        echo $book->translator->name;

        foreach ($book->tags as $tag) {
            echo $tag->name;
        }
    }
}
No. Location Link
1. Nextras View Full Documentation
2. GitHub View Repositoryl

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post