打印/下载
使用
查询
新增
更新
删除
回滚
其他

引入

use app\model\Demo;

实例化

$m = new Demo();

单条

Demo::findFirst(['uid=1'])->toArray();

多条

$res = Demo::find(['uid in (1,2,3)'])->toArray();

过滤

$res = Demo::find([
  'uid like :ids:',
  'bind'=>['ids'=>'1']
])->toArray();

链表查询

$builder = $this->modelsManager->createBuilder();
$builder->addfrom('app\model\Demo1', 'a');
$builder->leftJoin('app\model\Demo2', 'a.id=b.uid', 'b');
$builder->columns('a.name as name');
$data = $builder->getQuery()->execute()->toArray();

统计条数

Demo::count();

新增

$m = new Demo();
$m->title = '测试1';
$m->ctime = date('YmdHis');
$m->create();
// 返回自增ID
$id = $m->uid

更新

$m = new Demo();
$m->uid = 1;
$m->title = '测试2';
$m->utime = date('YmdHis');
$m->update();
  • 来自客户端参数请使用bind过滤!

删除

$m = Demo::findFirst(['uid=:id:','bind'=>['id'=>'1']]);
$res = $m->delete();
  • 来自客户端参数请使用bind过滤!

回滚

// 开始
$this->db->begin();
// 数据操作
$m = new Demo()
$m->id = 1;
$m->utime = "2020-12-12"
if(!$m->update()){
  // 回滚
  $this->db->rollback();
}
// 提交
$this->db->commit();

获取SQL

$query = $this->modelsManager->createQuery('select * from app\model\Demo');
$sql = $query->getSql();
self::bug($sql);

执行SQL

$query = $this->modelsManager->createQuery('select * from app\model\Demo');
$res = $query->execute();
self::bug($res->toArray());
{{copy}}