Skip to content

Thinkscape/ActiveRecord

Repository files navigation

ActiveRecord Build Status Coverage Status

Modern ActiveRecord implementation for PHP 5.4+

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.


Installation Quick Start Documentation

Installation

Requirements

Installation using Composer

  1. Inside your app directory run composer require thinkscape/activerecord:dev-master
  2. Make sure you are using composer autoloader: include "vendor/autoload.php";
  3. Follow quick start instructions.

Manual installation

  1. Obtain the source code by either:
  1. Set up class autoloading by either:
  • using the provided autoloader: require "init_autoload.php";, or
  • adding src directory as namespace Thinkscape\ActiveRecord to your existent autoloader.
  1. Follow quick start instructions.

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.

Using with Zend Framework 2

  1. Install source code using one of the above methods.
  2. Enable TsActiveRecord module in your config/application.config.php.
  3. Copy docs/activerecord.global.php.dist as config/autoload/activerecord.global.php inside your application dir.
  4. Edit config/autoload/activerecord.global.php and assign default db adapter.
  5. Read more about using ActiveRecord with ZF2

Using with Symfony 2

  1. Read more about using ActiveRecord with Symfony 2

Documentation

Quick Start

1) Make your classes ActiveRecords

ActiveRecord is used to add database functionality to your existing model classes. Let's create a simple active record class.

use Thinkscape\ActiveRecord;

class Country
{
    use ActiveRecord\Core;
    use ActiveRecord\Persistence\ZendDb;

    protected static $_dbTable    = 'countries';
    protected static $_properties = [ 'name', 'continent', 'population' ];
}

More info on configuring ActiveRecords

2) Connect to a database

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:

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

3) Insert, update and delete records

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

4) Retrieve records from database

$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

More info on queries and finding records

5) Add more features to your class

  • ActiveRecord\PropertyFilter

  • ActiveRecord\AttributeMethods

  • ActiveRecord\Aliasing

  • ActiveRecord\Aggregations

  • ActiveRecord\Associations

  • ActiveRecord\Conversion

  • ActiveRecord\CounterCache

  • ActiveRecord\Callbacks

  • ActiveRecord\Inheritance

  • ActiveRecord\Integration

  • ActiveRecord\Locking\Optimistic

  • ActiveRecord\Locking\Pessimistic

  • ActiveRecord\ModelSchema

  • ActiveRecord\NestedAttributes

  • ActiveRecord\Reflection

  • ActiveRecord\Readonly

  • ActiveRecord\ReadonlyAttributes

  • ActiveRecord\Scoping

  • ActiveRecord\Serialization

  • ActiveRecord\Timestamp

  • ActiveRecord\Transactions

  • ActiveRecord\Validations

About

Modern ActiveRecord implementation for PHP 5.4+

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages