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 |
---|
- PHP 5.4.0 or newer
- Database connection using one of the following
- 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.
- 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.
- 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
- Read more about using ActiveRecord with Symfony 2
- 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
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' ];
}
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);
// 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
$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\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