bephp/activerecord 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bephp/activerecord

Composer 安装命令:

composer require bephp/activerecord

包简介

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

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

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

中文版.

Documentation

Documentation

API Reference

CRUD functions

setDb(\PDO $db)

set the DB connection.

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.

$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".

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

findAll() : array

function to find all records in database. return array of ActiveRecord

$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.

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

delete() : boolean

function to delete current record in database.

reset() : \ActiveRecord

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

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.

SQL part functions

select()

function to set the select columns.

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

from()

function to set the table to find record

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

join()

function to set the table to find record

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

where()

function to set where conditions

$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.

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

bephp/activerecord 适用场景与选型建议

bephp/activerecord 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.08k 次下载、GitHub Stars 达 116, 最近一次更新时间为 2015 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「orm」 「micro」 「pdo」 「activerecord」 「relation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bephp/activerecord 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bephp/activerecord 我们能提供哪些服务?
定制开发 / 二次开发

基于 bephp/activerecord 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 120
  • 点击次数: 24
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 116
  • Watchers: 7
  • Forks: 19
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-22