micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).
中文版.
set the DB connection.
ActiveRecord::setDb(new PDO('sqlite:test.db'));
function to build insert SQL, and insert current record into database. if insert success return current object, other wise return false.
$user = new User();
$user->name = 'demo';
$user->password = md5('demo');
$user->insert();
function to find one record and assign in to current object. If call this function using $id param, will find record by using this id. If not set, just find the first record in database. if find record, assign in to current object and return it, other wise return "false".
$user->notnull('id')->orderby('id desc')->find();
function to find all records in database. return array of ActiveRecord
$user->findAll();
function to build update SQL, and update current record in database, just write the dirty data into database. if update success return current object, other wise return false.
$user->notnull('id')->orderby('id desc')->find();
$user->email = '[email protected]';
$user->update();
function to delete current record in database.
function to reset the $params and $sqlExpressions. return $this, can using chain method calls.
function to SET or RESET the dirty data. The dirty data will be set, or empty array to reset the dirty data.
function to set the select columns.
$user->select('id', 'name')->find();
function to set the table to find record
$user->select('id', 'name')->from('user')->find();
function to set the table to find record
$user->join('contact', 'contact.user_id = user.id')->find();
function to set where conditions
$user->where('id=1 AND name="demo"')->find();
$user->select('count(1) as count')->groupby('name')->findAll();
$user->orderby('name DESC')->find();
$user->orderby('name DESC')->limit(0, 1)->find();
$user->eq('id', 1)->find();
$user->ne('id', 1)->find();
$user->gt('id', 1)->find();
$user->lt('id', 1)->find();
$user->ge('id', 1)->find();
$user->le('id', 1)->find();
$user->like('name', 'de')->find();
$user->in('id', [1, 2])->find();
$user->notin('id', [1,3])->find();
$user->isnull('id')->find();
$user->isnotnull('id')->find();
composer require bephp/activerecord
There's one Blog demo, work with Router and MicoTpl.
include "ActiveRecord.php";
class User extends ActiveRecord{
public $table = 'user';
public $primaryKey = 'id';
public $relations = array(
'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),
'contact' => array(self::HAS_ONE, 'Contact', 'user_id'),
);
}
class Contact extends ActiveRecord{
public $table = 'contact';
public $primaryKey = 'id';
public $relations = array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'user_with_backref' => array(self::BELONGS_TO, 'User', 'user_id', array(), 'contact'),
// using 5th param to define backref
);
}
ActiveRecord::setDb(new PDO('sqlite:test.db'));
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY,
name TEXT,
password TEXT
);");
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS contact (
id INTEGER PRIMARY KEY,
user_id INTEGER,
email TEXT,
address TEXT
);");
$user = new User();
$user->name = 'demo';
$user->password = md5('demo');
var_dump($user->insert());
$contact = new Contact();
$contact->address = 'test';
$contact->email = '[email protected]';
$contact->user_id = $user->id;
var_dump($contact->insert());
$user = new User();
// find one user
var_dump($user->notnull('id')->orderby('id desc')->find());
echo "\nContact of User # {$user->id}\n";
// get contacts by using relation:
// 'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),
var_dump($user->contacts);
$contact = new Contact();
// find one contact
var_dump($contact->find());
// get user by using relation:
// 'user' => array(self::BELONGS_TO, 'User', 'user_id'),
var_dump($contact->user);