thinkscape / activerecord
Modern ActiveRecord implementation for PHP 5.4+
Requires
- php: >=5.4.3
Requires (Dev)
- doctrine/dbal: 2.3.*
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: ~0.6
- zendframework/zend-db: 2.2.*
Suggests
- doctrine/dbal: Doctrine DBAL integration.
- pecl-mongo: MongoDB integration
- zendframework/zend-db: Zend\Db integration.
This package is not auto-updated.
Last update: 2025-03-24 17:42:45 UTC
README
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
- PHP 5.4.0 or newer
- Database connection using one of the following
Installation using Composer
- Inside your app directory run
composer require thinkscape/activerecord:dev-master
- Make sure you are using composer autoloader:
include "vendor/autoload.php";
- Follow quick start instructions.
Manual installation
- Obtain the source code by either:
- cloning git project from github, or
- downloading and extracting source package.
- Set up class autoloading by either:
- using the provided autoloader:
require "init_autoload.php";
, or - adding
src
directory as namespaceThinkscape\ActiveRecord
to your existent autoloader.
- 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
- Install source code using one of the above methods.
- Enable
TsActiveRecord
module in yourconfig/application.config.php
. - Copy
docs/activerecord.global.php.dist
asconfig/autoload/activerecord.global.php
inside your application dir. - Edit
config/autoload/activerecord.global.php
and assign default db adapter. - Read more about using ActiveRecord with ZF2
Using with Symfony 2
- Read more about using ActiveRecord with Symfony 2
Documentation
- Quick Start
- Configuration
- CRUD - Create, Read, Update, Delete
- Queries and traversal
- Persistence methods and DB configuration
- Features and add-ons
- Theory and discussion on ActiveRecord pattern
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' ]; }
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);
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
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
5) Add more features to your class
-
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