Getting Started with Moldable: An ORM for continuous migration.

Happy Hacktoberfest!!!!!!

courtesy: Digital Ocean

As a developer, working with databases can be a real pain in the behind. You have to manually create models, make and create migrations, and manually handle the structure of your tables (external relationships, eager loading and a lot more). Various languages have different means of handling these processes, and in the last couple of years, ORMs have become the de-Facto method of interacting with databases for the following reasons:

1. They provide an abstraction layer for interacting with Databases (No need for writing SQL codes)

2. They are lightweight.

3. They separate the business logic from the actual code itself.

4. Gone are the days of mixing SQL codes with your PHP, JavaScript of C# codes.

Introducing Moldable.

Moldable, much like many ORMs out there is an abstraction layer to manage MySQL database with an improved function to alter-state and manipulate database schema. Moldable integrates an ORM class for manage persistent objects and adapt database schema. It is designed specifically for the PHP ecosystem and currently only provides support for MySQL database.

Getting started with Moldable for your project is pretty easy. You should have composer installed, and run the following command from your project directory:

composer require javanile/moldable

Configuring the connection to your database is pretty easy. Just create a configuration file and insert the following lines of code:

use Javanile\Moldable\Database
$db = new Database([
'host'     => 'localhost',
'dbname'   => 'db_marketing',
'username' => 'root',
'password' => 'p4ssw0rd',
'prefix'   => 'prefix_',

First, we import the moldable package, then we initialize a new database connection by specifying the host, username, password and prefix parameters. This is the file your application looks for first in its bid to create a connection to the database.

Creating a database schema using moldable is straightforward. Create a new PHP file in your models directory and insert these lines of code:

'Customer' => [
'id'     => $db::PRIMARY_KEY,       
'name'   => '',                     
'points' => 0,                      

'born'   => $db::DATE,              
'bio'    => $db::TEXT,              
'Products' => [
'id'    => $db::PRIMARY_KEY,        
'name'  => '',                      
'price' => .0,                      

The schema above is self-explanatory; our db variable has been created when we initialized the connection earlier. The apply method takes in the table definitions (in this case, Customer and Products) and their respective fields.

What about class models?

Moldable also provides schema support for creating class models via the ‘Storable’ namespace. The following snippet demonstrates that in effect:

use Javanile\Moldable\Storable;
class Customer extends Storable
public $id = self::PRIMARY_KEY;
public $name = '';
$customer = new Customer();
$customer->name = 'Franky Franco';

Using the class model, database tables and fields are generated automatically or updated if there is a change in the class definition. To get more information on the moldable ORM, you can check out the project’s Github repo and if you are an enthusiast, join in contributing to the project.

Happy Hacktoberfest once again!

Like what you read? Give Ayelegun Michael Kayode a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.