romagny13 / micro-db
MicroPHP 数据库库
0.0.2
2017-05-06 14:47 UTC
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-09-20 20:05:52 UTC
README
安装
composer require romagny13/micro-db
使用
配置数据库连接。
示例
$settings = [ 'dsn' =>"mysql:host=localhost;dbname=blog", 'username'=>'root', 'password' =>'' ]; Db::setConnectionStringSettings($settings['dsn'],$settings['username'],$settings['password']);
SQL 表/列策略
默认情况下,列被反引号包围
示例
select `posts`.`id`,`title`,`content`,`users`.`id`,`users`.`username` from `posts`,`users` order by `title`
更改策略。示例
Db::setTableAndColumnStrategy('[',']');
select [posts].[id],[title],[content],[users].[id],[users].[username] from [posts],[users] order by [title]
使用查询构建器
- select
- insert_into
- update
- delete
示例
选择
$posts = Db::getInstance() ->select('id','title','content','user_id') ->from('posts') ->where(Condition::op('user_id',1)) ->orderBy('title') ->limit(10) ->fetchAll();
其他示例,获取一个类
class Post { } $posts = Db::getInstance() ->select('posts.id','title','content','user_id','users.id','users.username') ->from('posts','users') ->where('user_id=1') ->orderBy(Sort::desc('title'),'content desc') ->limit(2,10) ->fetchAll(Post::class);
获取查询字符串
$queryString = Db::getInstance()->select('posts.id','title','content','user_id','users.id','users.username') ->from('posts','users') ->where('user_id=1') ->orderBy(Sort::desc('title'),'content desc') ->limit(2,10) ->build(); var_dump($queryString);
select `posts`.`id`,`title`,`content`,`user_id`,`users`.`id`,`users`.`username` from `posts`,`users` where user_id=1 order by `title` desc,`content` desc limit 2,10
插入
$success = Db::getInstance() ->insert_into('posts') ->columns('title','content','user_id') ->execute(['my title','my content',1]);
或
$success = Db::getInstance() ->insert_into('posts') ->columns('title','content','user_id') ->values('my title','my content',1) ->execute();
并获取最后一个插入的 ID
$id = Db::getInstance()->lastInsertId();
更新
$success = Db::getInstance() ->update('posts') ->set([ 'title'=>'new title', 'content' => 'new content' ]) ->where(Condition::op('id',1)) ->execute();
删除
$success = Db::getInstance() ->delete_from('posts') ->where(Condition::op('id',1)) ->execute();
条件助手
- 操作符
- in
- between
- like
通过使用
- 和
- 或
PDO
简单查询
$posts = Db::getInstance() ->query('select * from posts') ->fetchAllWithClass(Post::class);
带有参数的查询
$posts = Db::getInstance() ->prepare('select * from posts where id=:id') ->setParam(':id',1) ->fetchObject(Post::class);
其他示例
$success = Db::getInstance() ->prepare('insert into posts (title,content,user_id) values (?,?,?)') ->execute(['My title', 'My content',2]); $id = Db::getInstance()->lastInsertId();
或使用命名参数
$success = Db::getInstance() ->prepare('insert into posts (title,content,user_id) values (:title,:content,:user_id)') ->setParam(':title','My title') ->setParam(':content','My content') ->setParam(':user_id',2) ->execute(); $id = Db::getInstance()->lastInsertId();
模型
创建一个模型并定义数据库表。默认情况下,所有列都将被填充 ['*']。
use \MicroPHP\Db\Model; class PostModel extends Model { public function __construct() { $this->table = 'posts'; } }
定义要填充的列
use \MicroPHP\Db\Model; class PostModel extends Model { public function __construct() { $this->table = 'posts'; $this->columns = ['id','title','content','user_id']; } }
全部
获取表的全部记录
$posts = PostModel::all();
带有限制
示例:只返回 10 篇帖子(最大值)
$posts = PostModel::all(10);
带有偏移量 + 限制
示例:在 2 篇帖子之后只返回 10 篇帖子(最大值)
$posts = PostModel::all(2,10);
Where
允许选择要返回的记录(数组)
$posts = PostModel::where(Condition::op('user_id',1)->_or_(Condition::op('user_id',2)));
或使用字符串
$posts = PostModel::where('user_id=1 or user_id=2');
带有偏移量 + 限制
$posts = PostModel::where('user_id=1 or user_id=2',2,10);
查找
返回单个项目。
$post = PostModel::find(Condition::op('id',1));
创建
示例
$success = PostModel::create([ 'title' => 'My title', 'content' => 'My content', 'user_id' => 1 ]);
更新
示例
$success = PostModel::update([ 'title' => 'My new title', 'content' => 'My new content' ],Condition::op('id',1));
删除
示例
$success = PostModel::delete(Condition::op('id',1));
查询和准备
是 Db 函数的快捷方式。
关系
添加与其他模型的关系(0-1)或(1-1)
class UserModel extends Model { public function __construct() { $this->table = 'users'; } } class CategoryModel extends Model { public function __construct() { $this->table = 'categories'; } } class PostModel extends Model { public function __construct() { $this->table = 'posts'; $this->columns = ['title','content']; // relations $this->addRelation('users',['user_id' => 'id'],UserModel::class, 'user'); $this->addRelation('categories',['category_id' => 'id'],CategoryModel::class, 'category'); } }
addRelation 参数
- 外键表名
- 外键 => 主键对
- 要填充的模型
- 要添加的属性名
将用户和分类属性添加到帖子模型中。
示例
$post = PostModel::find(Condition::op('id',1));