How to use the ActiveRecord pattern (Eloquent) in Slim

Overview

Previously I showed you a way to integrate Doctrine in Slim Framework.

Now let’s look at how to integrate Eloquent into your project without Laravel.

What is ActiveRecord?

Active record is an architectural pattern, named by Martin Fowler in 2003, that stores in-memory object data in a relational database. The interface for this pattern includes CRUD methods and properties that map, more or less, one on one to the column in the underlying database.

This approach puts data access logic in the domain object. This way all people know how to read and write their data to and from the database.

This is also the main difference from Data Mapper pattern, where domain models don’t need to know how the data is saved.

Small talk about Eloquent, how to integrate

Eloquent is an Object Relational Mapping (ORM) package developed by Laravel, that follows the ActiveRecord pattern. As you would already noted, this package is the default ORM in Laravel projects, but you can use it without Laravel, since it can easily be integrated in your project, no matter which framework you are using. You just need to add “illuminate/database” package in your composer.json, editing this file or just running:

composer require illuminate/database

The next step is install and update the autoload classes

composer install composer dump-autoload -o

Now you have all in place and ready to start using it. Depend on which framework you’re working on, you may need to require the autoload file that will make classes and dependencies to be accessible.

require $PROJECT_ROOT.’/vendor/autoload.php';

Some frameworks will require you define some constants to establish database connection, For example in  Zend, you will have to define the following:

<?php 

defined(“DBDRIVER”)or define(‘DBDRIVER’,’mysql’);
defined(“DBHOST”)or define(‘DBHOST’,’localhost’);
defined(“DBNAME”)or define(‘DBNAME’,’eloquent-app’);
defined(“DBUSER”)or define(‘DBUSER’,’root’);
defined(“DBPASS”)or define(‘DBPASS’,’pass’);

After the definitions are created, create all models you need and extends them from \Illuminate\Database\Eloquent\Model. 

That’s it! you’re using Zend Framework + Eloquent.

How to install in slim

Slim frameworks is not different than others, just use composer to add package in vendors folder, and require the composer autoload in your project bootstrap file (in case this is the first package in your project)

Configuration

Now that you are already editing the bootstrap, let me illustrate the basic settings with an example I took form Slim documentaiton:

// Database information
$settings = array(
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'database' => '',
    'username' => '',
    'password' => '',
    'collation' => 'utf8_general_ci',
    'prefix' => ''
);

// Bootstrap Eloquent ORM
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory();
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

and voila, you are ready to start using Eloquent, just need to extends  \Illuminate\Database\Eloquent\Model and that’s it.

Integration  with erdiko-eloquent v2

Like in the other frameworks, we have to setup a database connection. With this purpose, you will find in the config folder of your project a PHP file named database.php that simple expose the settings returning them as array.
I strongly encourage not change it, but instead used the envvars to set the custom values.

Here’s and example:


<?php
/* Database config */
return [
   'default' => 'master',
   'meta' => [
       'entities' => [
           getenv("ERDIKO_ROOT").'/app/entities'
       ],
       'is_dev_mode' => getenv("ERDIKO_IS_DEV_MODE"),
       'cache' => null,
   ],
   'connections' => [
       'master' => [
           'driver' => 'pdo_mysql',
           'host' => getenv("DB_HOST"),
           'port' => getenv("DB_PORT"),
           'database' => getenv("DB_DATABASE"),
           'username' => getenv("DB_USERNAME"),
           'password' => getenv("DB_PASSWORD"),
           'charset' => 'utf8',
           'collation' => 'utf8_unicode_ci',
           'prefix' => ''
       ]
   ]
];

Once we have the database settings ready, the Erdiko-eloquent package do the magic of connect database with the ORM. Now since this is based on ActiverRecord pattern that we already discussed, you just have to create models, that match you needs, and make them extends \erdiko\eloquent\Model and happy coding!

Final thoughts

If you like ActiveRecord pattern, whether its because of your background in other frameworks or languages, like Ruby on Rails, or because this is what best fit to your current application, then I strongly recommend Eloquent. This is a robust ORM that has been around for a while and has a large community that would help in case you need.
In other hand, if your project needs something different, you are familiar with DataMapper (again because your background was in Symfony or Java, just to mention few examples) or you just want to learn something new, please take a look at How to use the DataMapper pattern (Doctrine) in Slim

So, you’re still here!! Thank you for reading, hope you enjoyed and see you in the next episode!

Next Post

Comments

See how we can help

Lets talk!

Stay up to date on the latest technologies

Join our mailing list, we promise not to spam.