An introduction to ActiveRecord

Connections, models and active records are the foundations of everything that concerns database access and management. They are used to establish database connections, manage tables and their possible relationship, as well as manage the records of these tables. Leveraging OOP, the models and active records are instances which properties, getters/setters and behavior can be inherited in a business logic.

Using the query interface, you won't have to write raw SQL, manage table relationship, or worry about injection.

Finally, using providers you can define all your connections and models in a single place. Connections are established and models are instantiated on demand, so feel free the define hundreds of them.

Bindings

The icanboogie/bind-activerecord package binds the icanboogie/activerecord package to ICanBoogie, using its Autoconfig feature. It provides configuration synthesizers for connections and models, as well as getters for connection collection and model collection. A model provider is also declared:

<?php
namespace ICanBoogie;

$app = boot();

$connections_config = $app->configs['activerecord_connections'];
$models_config = $app->configs['activerecord_models'];

echo get_class($app->connections);               // ICanBoogie\ActiveRecord\ConnectionCollection
echo get_class($app->models);                    // ICanBoogie\ActiveRecord\ModelCollection

$primary_connection = $app->connections['primary'];
$primary_connection === $app->db;                // true

get_models('nodes') === $app->models['nodes'];   // true

Autoconfig

ICanBoogie's Autoconfig is used to provide the following features:

The activerecord config fragment

Currently activerecord fragments are used to synthesize activerecord_connections and activerecord_models configurations, which are suitable to create ConnectionCollection and ModelCollection instances.

The following example demonstrates how to define connections and models. Two connections are defined: primary is a connection to the MySQL server;cache is a connection to a SQLite database. The nodes model is also defined.

<?php

// config/activerecord.php

use ICanBoogie\ActiveRecord\ConnectionOptions;
use ICanBoogie\ActiveRecord\Model;

return [

    'connections' => [

        'primary' => [

            'dsn' => 'mysql:dbname=mydatabase',
            'username' => 'root',
            'password' => 'root',
            'options' => [

                ConnectionOptions::TIMEZONE => '+02:00',
                ConnectionOptions::TABLE_NAME_PREFIX => 'myprefix'

            ]
        ],

        'cache' => 'sqlite:' . ICanBoogie\REPOSITORY . 'cache.sqlite'

    ],

    'models' => [

        'nodes' => [

            Model::SCHEMA => [

                'id' => 'serial',
                'title' => 'varchar'

            ]
        ]
    ]
];

Exceptions

ActiveRecord exceptions implement the ICanBoogie\ActiveRecord\Exception interface so that they can easily be identified:

<?php

try
{
    // …
}
catch (\ICanBoogie\ActiveRecord\Exception $e)
{
    // an ActiveRecord exception
}
catch (\Exception $e)
{
    // some other exception
}

The following exceptions are defined: