2017 © Pedro Peláez
 

library activerecord

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

image

bephp/activerecord

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  • Thursday, March 1, 2018
  • by lloydzhou
  • Repository
  • 8 Watchers
  • 109 Stars
  • 1,403 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 20 Forks
  • 0 Open issues
  • 17 Versions
  • 1 % Grown

The README.md

activerecord

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO)., (*2)

中文版., (*3)

Documentation

Documentation, (*4)

API Reference

CRUD functions

setDb(\PDO $db)

set the DB connection., (*5)

ActiveRecord::setDb(new PDO('sqlite:test.db'));

insert() : boolean|\ActiveRecord

function to build insert SQL, and insert current record into database. if insert success return current object, other wise return false., (*6)

$user = new User();
$user->name = 'demo';
$user->password = md5('demo');
$user->insert();

find(integer $id = null) : boolean|\ActiveRecord

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"., (*7)

$user->notnull('id')->orderby('id desc')->find();

findAll() : array

function to find all records in database. return array of ActiveRecord, (*8)

$user->findAll();

update() : boolean|\ActiveRecord

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., (*9)

$user->notnull('id')->orderby('id desc')->find();
$user->email = 'test@example.com';
$user->update();

delete() : boolean

function to delete current record in database., (*10)

reset() : \ActiveRecord

function to reset the $params and $sqlExpressions. return $this, can using chain method calls., (*11)

dirty(array $dirty = array()) : \ActiveRecord

function to SET or RESET the dirty data. The dirty data will be set, or empty array to reset the dirty data., (*12)

SQL part functions

select()

function to set the select columns., (*13)

$user->select('id', 'name')->find();

from()

function to set the table to find record, (*14)

$user->select('id', 'name')->from('user')->find();

join()

function to set the table to find record, (*15)

$user->join('contact', 'contact.user_id = user.id')->find();

where()

function to set where conditions, (*16)

$user->where('id=1 AND name="demo"')->find();

group()/groupby()

$user->select('count(1) as count')->groupby('name')->findAll();

order()/orderby()

$user->orderby('name DESC')->find();

limit()

$user->orderby('name DESC')->limit(0, 1)->find();

WHERE conditions

equal()/eq()

$user->eq('id', 1)->find();

notequal()/ne()

$user->ne('id', 1)->find();

greaterthan()/gt()

$user->gt('id', 1)->find();

lessthan()/lt()

$user->lt('id', 1)->find();

greaterthanorequal()/ge()/gte()

$user->ge('id', 1)->find();

lessthanorequal()/le()/lte()

$user->le('id', 1)->find();

like()

$user->like('name', 'de')->find();

in()

$user->in('id', [1, 2])->find();

notin()

$user->notin('id', [1,3])->find();

isnull()

$user->isnull('id')->find();

isnotnull()/notnull()

$user->isnotnull('id')->find();

Install

composer require bephp/activerecord 

There's one Blog demo, work with Router and MicoTpl., (*17)

Demo

Include base class ActiveRecord

include "ActiveRecord.php";

Define Class

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
    );
}

Init data

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
                        );");

Insert one User into database.

$user = new User();
$user->name = 'demo';
$user->password = md5('demo');
var_dump($user->insert());

Insert one Contact belongs the current user.

$contact = new Contact();
$contact->address = 'test';
$contact->email = 'test1234456@domain.com';
$contact->user_id = $user->id;
var_dump($contact->insert());

Example to using relations

$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);

The Versions

01/03 2018

dev-master

9999999-dev http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

01/03 2018

v2.2.1

2.2.1.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

28/02 2018

v2.2.0

2.2.0.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

22/10 2016

v2.1.1

2.1.1.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

25/08 2016

v2.1.0

2.1.0.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

25/08 2016

dev-feature-backref

dev-feature-backref http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

11/05 2016

v2.0.9

2.0.9.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

11/04 2016

v2.0.8

2.0.8.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

01/04 2016

v2.0.7

2.0.7.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

22/10 2015

v2.0.6

2.0.6.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

16/10 2015

v2.0.5

2.0.5.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

12/10 2015

v2.0.4

2.0.4.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

orm database micro pdo activerecord relation

07/10 2015

dev-nocomments

dev-nocomments http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm database micro pdo activerecord relation

26/09 2015

v2.0.3

2.0.3.0 http://lloydzhou.github.io/activerecord/

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS_ONE, HAS_MANY, BELONGS_TO).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm database micro pdo activerecord relation

23/09 2015

v2.0.2

2.0.2.0 http://lloydzhou.github.io/activerecord/

simple activerecord in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord

15/09 2015

v2.0.1

2.0.1.0 http://lloydzhou.github.io/activerecord/

simple activerecord in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord

15/09 2015

v2

2.0.0.0 http://lloydzhou.github.io/activerecord/

simple activerecord in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord