dev-master
9999999-dev https://github.com/Thinkscape/ActiveRecordModern ActiveRecord implementation for PHP 5.4+
MIT
The Requires
- php >=5.4.3
The Development Requires
orm activerecord db active record
Modern ActiveRecord implementation for PHP 5.4+
Modern ActiveRecord implementation for PHP 5.4+, (*1)
It is simple to use, easy to maintain and performant when used together with well-designed userland code. ActiveRecord is an architectural pattern for adding database CRUD functionality to domain objects., (*2)
, (*3)
| Installation | Quick Start | Documentation |, (*4)
composer require thinkscape/activerecord:dev-master
include "vendor/autoload.php";
require "init_autoload.php";
, orsrc
directory as namespace Thinkscape\ActiveRecord
to your existent autoloader.Before you jump into quick start, make sure you are using PHP 5.4, you have installed the component into your application and you have included Composer autoloader or the included autoload_register.php., (*5)
TsActiveRecord
module in your config/application.config.php
.docs/activerecord.global.php.dist
as config/autoload/activerecord.global.php
inside your application dir.config/autoload/activerecord.global.php
and assign default db adapter.ActiveRecord is used to add database functionality to your existing model classes. Let's create a simple active record class., (*6)
use Thinkscape\ActiveRecord; class Country { use ActiveRecord\Core; use ActiveRecord\Persistence\ZendDb; protected static $_dbTable = 'countries'; protected static $_properties = [ 'name', 'continent', 'population' ]; }
All persistence methods (such as ZendDb, DoctrineDBAL, ...) require a working database connection. We have to create a new connection adapter and configure it with ActiveRecord:, (*8)
use Zend\Db\Adapter\Adapter; // Create Zend\Db MySQLi adapter $adapter = new Adapter(array( 'driver' => 'Mysqli', 'database' => 'my_application', 'username' => 'developer', 'password' => 'developer-password' )); // Method 1. Set default adapter for all ActiveRecord instances Thinkscape\ActiveRecord\Persistence\ZendDb::setDefaultDb($adapter); // Method 2. Set default adapter for Country class Country::setDefaultDb($adapter); // Method 3. Create an instance and assign an adapter to it $finland = new Country(); $finland->setDb($adapter);
More info on persistence methods and configuring database, (*9)
// Create new record $finland = new Country(); $finland->setName('Finland'); $finland->save(); // INSERT INTO country (name) VALUES ("Finland") // Update $finland->setName('Maamme'); $finland->save(); // UPDATE country SET name = "Maamme" // Delete $finland->delete(); // DELETE FROM country WHERE id = 1
More info on CRUD operations, (*10)
$first = Country::findFirst(); // SELECT * FROM country ORDER BY id ASC LIMIT 1 $countryById = Country::findById(220); // SELECT * FROM country WHERE id = 220 $countryByName = Country::findOneBy('name', 'Finland'); // SELECT * FROM country WHERE name = "Finland" LIMIT 1 $countryByName = Country::findOne([ 'name' => 'Finland' ]); // SELECT * FROM country WHERE name = "Finland" LIMIT 1 $allEuropeanCountries = Country::findAll([ 'continent' => 'Europe' ]); // SELECT * FROM country WHERE continent = "Finland" $allBigCountries = Country::findAll([ ['population', 'gt', 30000000] ]); // SELECT * FROM country WHERE population >= 30000000
ActiveRecord\PropertyFilter, (*12)
ActiveRecord\AttributeMethods, (*13)
Modern ActiveRecord implementation for PHP 5.4+
MIT
orm activerecord db active record