Binds ActiveRecord to ICanBoogie

v4.0.0 2018-03-17 23:38 UTC

This package is auto-updated.

Last update: 2020-11-07 13:21:36 UTC


Release Build Status Code Quality Code Coverage Packagist

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. The get_model() helper is also patched to use the model collection bound to the application.

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


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

  • A synthesizer for the activerecord_connections config, created from the activerecord#connections fragments.
  • A synthesizer for the activerecord_models config, created from the activerecord#models fragments.
  • A lazy getter for the ICanBoogie\Application::$connections property, that returns a ConnectionCollection instance created with the activerecord_connections config.
  • A lazy getter for the ICanBoogie\Application::$models property, that returns a ModelCollection instance created with the activerecord_models config.
  • A lazy getter for the ICanBoogie\Application::$db property, that returns the connection named primary from the ICanBoogie\Application::$connections property.

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.


// 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'



The package requires PHP 5.6 or later.


The recommended way to install this package is through Composer:

$ composer require icanboogie/bind-activerecord

Cloning the repository

The package is available on GitHub, its repository can be cloned with the following command line:

$ git clone https://github.com/ICanBoogie/bind-activerecord.git


The package is documented as part of the ICanBoogie framework documentation. You can generate the documentation for the package and its dependencies with the make doc command. The documentation is generated in the build/docs directory. ApiGen is required. The directory can later be cleaned with the make clean command.


The test suite is ran with the make test command. PHPUnit and Composer need to be globally available to run the suite. The command installs dependencies as required. The make test-coverage command runs test suite and also creates an HTML coverage report in "build/coverage". The directory can later be cleaned with the make clean command.

The package is continuously tested by Travis CI.

Build Status Code Coverage


icanboogie/bind-activerecord is licensed under the New BSD License - See the LICENSE file for details.