icanboogie / bind-activerecord
Binds ActiveRecord to ICanBoogie
Installs: 1 554
Dependents: 2
Suggesters: 2
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
- icanboogie/activerecord: ^5.0
- icanboogie/icanboogie: ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2023-09-11 11:35:40 UTC
README
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.
<?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:
- A synthesizer for the
activerecord_connections
config, created from theactiverecord#connections
fragments. - A synthesizer for the
activerecord_models
config, created from theactiverecord#models
fragments. - A lazy getter for the
ICanBoogie\Application::$connections
property, that returns a ConnectionCollection instance created with theactiverecord_connections
config. - A lazy getter for the
ICanBoogie\Application::$models
property, that returns a ModelCollection instance created with theactiverecord_models
config. - A lazy getter for the
ICanBoogie\Application::$db
property, that returns the connection namedprimary
from theICanBoogie\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.
<?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' ] ] ] ];
Requirements
The package requires PHP 7.2 or later.
Installation
composer require icanboogie/bind-activerecord
Documentation
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.
Testing
Run make test-container
to create and log into the test container, then run make test
to run the
test suite. Alternatively, run make test-coverage
to run the test suite with test coverage. Open
build/coverage/index.html
to see the breakdown of the code coverage.
License
icanboogie/bind-activerecord is released under the New BSD License.