2017 © Pedro Peláez
 

library think-orm

think orm

image

topthink/think-orm

think orm

  • Tuesday, July 31, 2018
  • by topthink
  • Repository
  • 6 Watchers
  • 110 Stars
  • 2,290 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 29 Forks
  • 6 Open issues
  • 21 Versions
  • 56 % Grown

The README.md

think-orm

基于PHP5.6+ 的ORM实现,主要特性:, (*1)

  • 基于ThinkPHP5.1的ORM独立封装
  • 支持Mysql、Pgsql、Sqlite、SqlServer、Oracle和Mongodb
  • 支持Db类和查询构造器
  • 支持事务
  • 支持模型和关联

适用于不使用ThinkPHP框架的开发者。, (*2)

安装, (*3)

composer require topthink/think-orm

Db类用法:, (*4)

use think\Db;
// 数据库配置信息设置(全局有效)
Db::setConfig(['数据库配置参数(数组)']);
// 进行CURD操作
Db::table('user')
    ->data(['name'=>'thinkphp','email'=>'thinkphp@qq.com'])
    ->insert(); 
Db::table('user')->find();
Db::table('user')
    ->where('id','>',10)
    ->order('id','desc')
    ->limit(10)
    ->select();
Db::table('user')
    ->where('id',10)
    ->update(['name'=>'test']); 
Db::table('user')
    ->where('id',10)
    ->delete();

Db类增加的(静态)方法包括: - setConfig 设置全局配置信息 - getConfig 获取数据库配置信息 - setQuery 设置数据库Query类名称 - setCacheHandler 设置缓存对象Handler(必须支持get、set及rm方法) - getSqlLog 用于获取当前请求的SQL日志信息(包含连接信息), (*5)

其它操作参考TP5.1的完全开发手册数据库章节, (*6)

定义模型:, (*7)

namespace app\index\model;
use think\Model;
class User extends Model
{
}

代码调用:, (*8)

use app\index\model\User;

$user = User::get(1);
$user->name = 'thinkphp';
$user->save();

Db类和模型对比使用

:white_check_mark: 创建Create

  • Db用法, (*9)

    Db::table('user')
        ->insert([
            'name'  => 'thinkphp',
            'email' => 'thinkphp@qq.com',
        ]);
    
  • 模型用法, (*10)

    $user        = new User;
    $user->name  = 'thinkphp';
    $user->email = 'thinkphp@qq.com';
    $user->save();
    
  • 或者批量设置, (*11)

    $user = new User;
    $user->save([
        'name'  => 'thinkphp',
        'email' => 'thinkphp@qq.com',
    ]);
    

:white_check_mark: 读取Read

  • Db用法, (*12)

    $user = Db::table('user')
        ->where('id', 1)
        ->find();
    //  或者
    $user = Db::table('user')
        ->find(1);
    echo $user['id'];
    echo $user['name'];
    
  • 模型用法, (*13)

    $user = User::get(1);
    echo $user->id;
    echo $user->name;
    
  • 模型实现读取多个记录, (*14)

    // 查询用户数据集
    $users = User::where('id', '>', 1)
        ->limit(5)
        ->select();
    
    // 遍历读取用户数据
    foreach ($users as $user) {
        echo $user->id;
        echo $user->name;
    }
    

:white_check_mark: 更新Update

  • Db用法, (*15)

    Db::table('user')
        ->where('id', 1)
        ->update([
            'name'  => 'topthink',
            'email' => 'topthink@qq.com',
        ]);
    
  • 模型用法, (*16)

    $user        = User::get(1);
    $user->name  = 'topthink';
    $user->email = 'topthink@qq.com';
    $user->save();
    
  • 或者使用, (*17)

    $user = User::get(1);
    $user->save([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ]);
    
  • 静态调用, (*18)

    User::update([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ], ['id' => 1]);
    

:white_check_mark: 删除Delete

  • Db用法, (*19)

    Db::table('user')->delete(1);
    
  • 模型用法, (*20)

    $user = User::get(1);
    $user->delete();
    
  • 或者静态实现, (*21)

    User::destroy(1);
    
  • 静态调用, (*22)

    User::update([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ], ['id' => 1]);
    
  • destroy方法支持删除指定主键或者查询条件的数据, (*23)

    // 根据主键删除多个数据
    User::destroy([1, 2, 3]);
    // 指定条件删除数据
    User::destroy([
        'status' => 0,
    ]);
    // 使用闭包条件
    User::destroy(function ($query) {
        $query->where('id', '>', 0)
            ->where('status', 0);
    });
    

    更多模型用法可以参考5.1完全开发手册的模型章节, (*24)

The Versions

31/07 2018

dev-master

9999999-dev

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

03/06 2018

v1.2.8

1.2.8.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

25/05 2018

v1.2.7

1.2.7.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

25/04 2018

v1.2.6

1.2.6.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

21/04 2018

v1.2.5

1.2.5.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

05/02 2018

v1.2.4

1.2.4.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

31/01 2018

v1.2.3

1.2.3.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

30/01 2018

v1.2.2

1.2.2.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

20/01 2018

v1.2.1

1.2.1.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

19/01 2018

v1.2

1.2.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

14/01 2018

v1.1

1.1.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

05/01 2018

v1.0

1.0.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

08/12 2017

v0.9

0.9.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

08/12 2017

v0.8

0.8.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

04/12 2017

v0.7

0.7.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

28/11 2017

0.6

0.6.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

24/11 2017

0.5

0.5.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

23/11 2017

0.4

0.4.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

20/11 2017

0.3

0.3.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

27/10 2017

0.2

0.2.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st

27/10 2017

0.1

0.1.0.0

think orm

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

by liu21st