PHP Swoft2 框架精华系列:数据库操作

发布于:2025-06-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

模型

Entity 模型的属性,PHP 7.4 或者以上版本要求类属性需要有明确的类型,需要注意,加了类型限制之后,一定要给默认值(null)。因为模型中在遍历模型的属性的时候,可能会对所有属性调用 get 方法,如果属性没有默认值,调用 get 的话,php 会报错,使用属性前需要对属性进行初始化。所以,通过工具 devtool 生成的默认的Entity 模版可能不是目前最优选择。同时,在生成 get set 方法的时候,也要兼容 null 类型,保证代码执行过程中不会出现上面的错误。

所有的 @Entity 注解标注的类,最后都会生成一个作用范围为 Bean::PROTOTYPE类型的 bean 实例存储在容器中。可以通过 bean() 方法直接获取到。

新增

对象方式
// 获取一个 User 实体的实例
$user = User::new();
$user->setId(1);
$user->setName('name');
$user->setPassword('password');
// 会在数据库新增一条数据
$user->save();
数组方式
// 存储数据的数组,键名和Entity 的属性名对应
$attributes = [
    'id' => 2,
    'name' => 'name2',
    'password' => 'password'
];
User::new($attributes)->save();
批量插入
// 要批量插入的数据数组.
$list = [
    ['id' => 3, 'name' => 'name3', 'password' => 'password3'],
    ['id' => 4, 'name' => 'name4', 'password' => 'password4'],
    ['id' => 5, 'name' => 'name5', 'password' => 'password5'],
];
// 开始批量插入
User::insert($list);

查询

常用的查询函数示例
// 查找主键值为 $id 的记录,$column 为 select 的列名称
User::find($id, array|string[] $columns = ['*']);

// 查询主键值为 $ids 数组的记录,相当于 in(id1, id2)
User::findMany($ids, array|string[] $columns = ['*']);

// 查找一条主键值为 $id 的记录,找不到则返回一个 User 的新Entity实例
User::findOrNew($id, array|string[] $columns = ['*']): Model

// 查找一条主键值为 $id 的记录,找不到会抛出一个异常
// throw new DbException('Model is not finded');
User::findOrFail($id, array|string[] $columns = ['*']): Model
查询单个数据
// 方法 1
$user =  User::find(1, ['id','name']);
// 方法 2
$user = User::where('id',1)->first();
查询多个数据
// 方法 1
$users = User::findMany([1,2,3,4], ['id','name']);
// 方法 2
$useer = User::whereIn('id', [1,2,3,4])->select(['id','name'])->get();

如果需要获取实体对象,可以使用 getModels 方法,返回的是一个实体的数组:

$users = User::where('id', 22)->getModels(['id', 'age']);
/* @var User $user */
foreach ($users as $user) {
    $age = $user->getAge();
}

有时候我我们需要按数据表某个键作为数组的键名,可以使用 CollectionkeyBy 方法。

假如 需要第一页 数据需要以id作为key 你可以这样使用:

// 生成一个键名为 id 值为 User 对象的 hash 表。
$users = User::forPage(1, 10)->get(['id', 'age'])->keyBy('id');
/* @var User $user */
foreach ($users as $id => $user) {
    $age = $user->getAge();
}

更新

Entity 方式更新
// 注意如果直接给值,查询的是主键
$user = User::find($id);
$name   = uniqid();
$user->setAge(1);
$result = $user->update(['name' => $name]);
条件更新
$wheres   = [
    'name' => 'sakuraovq',
    ['id', '>=', 2]
];
$orWheres = [
    ['status', '<>', 1']
];
$result = User::where($wheres)
                ->limit(1)
                ->orWhere($orWheres)
                ->update(['name' => 'sakuraovq' . mt_rand(1, 10)]);
更新或插入

存在即更新,不存在即插入新数据。

updateOrCreate()返回一个Entity

$user = User::updateOrCreate(['id' => 1], ['age' => 18, 'name' => 'sakuraovq']);
echo $user->getName();

updateOrInsert()返回一个bool值:

$isOk = User::updateOrInsert(['id' => 1], ['age' => 18, 'name' => 'sakuraovq']);

删除

指定删除数据
$user = User::find($id);
$result = $user->delete();
条件删除
// 条件删除所有
$result = User::where('id', 1)->delete();
// 删除一条
$result = User::where('stauts',1 )->limit(1)->delete();

其他

获取模型中的数据
  • Entity 实体中的属性
  • 通过 setModelAttribute(string $key, $value) 方法动态设置的属性
// 获取一个模型中已有数据
$user = User::new();
$user->setId('1');
$user->setName('name');
$user->setPassword('12345678');
// 此方法返回模型中的有值(非null)的属性,注意此方法不会管属性是不是有@Hidden 注解
$user->getModelAttributes();
// 返回模型所有的动态的属性,和模型中静态属性
$user->getArrayableAttributes();
// 返回模型中过滤掉 @Hidden 标记(隐藏)的属性
$user->attributesToArray();

查询器

简介

Swoft 的数据库查询构造器为创建和运行数据库查询提供了一个方便的接口。它可用于执行应用程序中大部分数据库操作,且可在所有支持的数据库系统上运行。

Swoft 的查询构造器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。因此没有必要清理作为绑定传递的字符串。

你可以使用 DB::table('xxxx')得到一个 Builder 对象 也可以使用
Builder::new()->from('xxx') 这两种写法返回结果是一样的,Builder对象不会分配连接,只有执行 sql 的时候才会从连接池从获取。

结果获取

从一个数据表中获取所有行

你可以 DB 上使用 table 方法来开始查询。该 table 方法为给定的表返回一个查询构造器实例,允许你在查询上链式调用更多的约束,最后使用 get 方法获取结果:

// sql = select * from `user`
$users = DB::table('user')->get();

get 方法返回一个包含 Collection 的结果,其中每个结果都是 StdClass 对象的一个实例。你可以访问字段作为对象的属性来访问每列的值:

foreach ($users as $user) {
    echo $user->name;
}

你还可以使用 Collection 所有方法 Collection 提供了强大的对象操作方法。

如果想更快的查询全部的数据可以用cursor方法,底层采用 yield 实现。其中每个结果都是 StdClass 对象的一个实例

$users = DB::table('user')->cursor();
foreach ($users as $user){
    echo $user->name;
}

从数据表中获取单行或单列

如果你只需要从数据表中获取一行数据,你可以使用 first 方法。该方法返回一个 StdClass 对象:

$user = DB::table('user')->where('name', 'Sakura')->first();
if ($user) {    
    echo $user->name;
}

如果你甚至不需要整行数据,则可以使用 value 方法从记录中获取单个值。该方法将直接返回该字段的值:

$name = DB::table('users')->where('name', 'Sakura')->value('name');

获取一列的值

如果你想获取包含单列值的集合,则可以使用 pluck 方法。在下面的例子中,我们将获取角色表中标题的集合

$titles = DB::table('roles')->pluck('title');
foreach ($roles as $title) {    
    echo $title;
}

你还可以在返回的集合中指定字段的自定义键值:

$roles = DB::table('users')->pluck('email', 'name');
foreach ($roles as $name => $email) {    
    echo $email;
}
分块结果

如果你需要处理上千条数据库记录,你可以考虑使用 chunk 方法。该方法一次获取结果集的一小块,并将其传递给 闭包 函数进行处理。在修复数据的时候就很适用。例如,我们可以将全部 user 表数据切割成一次处理 100 条记录的一小块:

DB::table('users')->orderBy('id')
    ->chunk(100, function (\Swoft\Stdlib\Collection $users) {   	 
        foreach ($users as $user) {       
		echo $user->name;
	}
});

你可以通过在 闭包 中返回 false 来终止继续获取分块结果:

DB::table('users')->orderBy('id')->chunk(100, function (\Swoft\Stdlib\Collection $users) {    // Process the records...    return false;});

闭包里面传递的 $users 是一个Collection对象,each方法也是通过 chunk 实现的 只是参数不同位置相反。

聚合

查询构造器还提供了各种聚合方法,比如 count, maxminavg,还有 sum。你可以在构造查询后调用任何方法:

$userNum = DB::table('users')->count();
$price   = DB::table('orders')->max('price');

当然,你也可以将这些聚合方法与其他的查询语句相结合:

$price = DB::table('orders')
    ->where('status', 1)
    ->avg('price');

如果没有查询到任何数据 返回值是一个 nullavgaverage方法的别名,而已返回是一个 float 类型。

判断记录是否存在

除了通过 count 方法可以确定查询条件的结果是否存在之外,还可以使用 existsdoesntExist 方法:

return DB::table('orders')->where('id', 1)->exists();
return DB::table('orders')->where('id', 1)->doesntExist();

查询

指定一个 Select 语句

当然你可能并不总是希望从数据库表中获取所有列。使用 select 方法,你可以自定义一个 select 查询语句来查询指定的字段:

// select `name`, `age` as `user_age` from `user`
$users = DB::table('user')->select('name', 'age as user_age')->get();

distinct 方法会强制让查询返回的结果不重复:

$users = DB::table('users')->distinct()->get();

如果你已经有了一个查询构造器实例,并且希望在现有的查询语句中加入一个字段,那么你可以使用 addSelect 方法:

$query = DB::table('users')->select('name');$users = $query->addSelect(['age'])->get();

原生表达式

 // select count(*) as `user_count`, `name` from `user`
 $users = DB::table('user')
                     ->selectRaw('count(*) as `user_count`, `name`'));
                     ->get();

注意:原生表达式将会被当做字符串注入到查询中,因此你应该小心使用,避免创建 SQL 注入的漏洞。

whereRaw / orWhereRaw

whereRaw 和 orWhereRaw 方法将原生的 where
注入到你的查询中。这两个方法的第二个参数还是可选项,值还是绑定参数的数组:

// select `name`, `age` as `user_age` from `user` where age > 18
$users = DB::table('user')
    ->whereRaw('age > :age', ['age' => 18])
    ->select('name', 'age as user_age')
    ->get();

havingRaw / orHavingRaw

havingRaw 和 orHavingRaw 方法可以用于将原生字符串设置为 having 语句的值:

 $orders = DB::table('user')
            ->selectRaw('sum(age) as age')
            ->groupBy('user_desc')
            ->havingRaw('age > ?', [17])
            ->get();

orderByRaw

orderByRaw 方法可用于将原生字符串设置为 ,order by 子句的值:

$time = time();
$orderBy = 'if(`dead_time`>' . $time . ', update_time,0) desc,create_time desc'; 
$orders = DB::table('ticket')
                ->orderByRaw($orderBy)
                ->get();

fromRaw

fromRaw 方法的自定义 FROM 关键字参数,比如使用强制索引:

  $sql = DB::table('')
            ->select('id', 'name')
            ->fromRaw('`user` force index(`idx_user`)')
            ->get();

Joins(联合查询)

Inner Join Clause

查询构造器也可以编写 join 方法。若要执行基本的
「内链接」,你可以在查询构造器实例上使用 join 方法。传递给 join 方法的第一个参数是你需要连接的表的名称,而其他参数则使用指定连接的字段约束。

你还可以在单个查询中连接多个数据表:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

Left Join 语句

如果你想使用 「左连接」代替「内连接」 ,可以使用 leftJoin 方法。leftJoin 方法与 join 方法用法相同:

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

Cross Join 语句

使用 crossJoin 方法和你想要连接的表名做 「交叉连接」。交叉连接在第一个表和被连接的表之间会生成笛卡尔积:

// select * from `user` cross join `count` on `count`.`user_id` = `user`.`id`
$users =Builder::new()
            ->from('user')
            ->crossJoin('count', 'count.user_id', '=', 'user.id')
            ->get();

高级 Join 语句

你可以指定更高级的 join 语句。比如传递一个 闭包 作为 join 方法的第二个参数。此 闭包 接收一个
JoinClause 对象,从而指定 join 语句中指定的约束

DB::table('users')
        ->join('contacts', function ($join) {
            $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
        })
        ->get();

如果你想要在连接上使用「where」 风格的语句,你可以在连接上使用 whereorWhere 方法。这些方法会将列和值进行比较,而不是列和列进行比较:

DB::table('users')
        ->join('contacts', function ($join) {
            $join->on('users.id', '=', 'contacts.user_id')
                 ->where('contacts.user_id', '>', 5);
        })
        ->get();

子连接查询

你可以使用 joinSubleftJoinSubrightJoinSub 方法关联一个查询作为子查询。他们每一种方法都会接收三个参数:子查询,表别名和定义关联字段的闭包:

$latestPosts = DB::table('posts')
                   ->select('MAX(created_at) as last_created_at')
                   ->where('is_published', true)
                   ->groupBy('user_id');
// $latestPosts 是一个 query 对象
$users = DB::table('users')
        ->joinSub($latestPosts, 'latest_posts', function($join) {
            $join->on('users.id', '=', 'latest_posts.user_id');
        })->get();

Unions

查询构造器还提供了将两个查询 「联合」 的快捷方式。比如,你可以先创建一个查询,然后使用 union 方法将其和第二个查询进行联合:

// (select * from `user`) union all (select * from `user`) union (select * from `user`)
Builder::new()
    ->from('user')
    ->unionAll(function (Builder $builder) {
        $builder->from('user');
    })
    ->union(Builder::new()->from('user'))
    ->get();

提示:也可以使用 unionAll 方法,用法 union 方法是的一样。

Where 条件

简单的 Where 语句

在构造 where 查询实例的中,你可以使用 where 方法。调用 where 最基本的方式是需要传递三个参数:第一个参数是列名,第二个参数是任意一个数据库系统支持的运算符,第三个是该列要比较的值。

例如,下面是一个要验证 「money」 字段的值等于 100 的查询:

$users = DB::table('user')->where('money', '=', 100)->get();

为了方便,如果你只是简单比较列值和给定数值是否相等,可以将数值直接作为 where 方法的第二个参数:

$users = DB::table('users')->where('votes', 100)->get();

当然,你也可以使用其他的运算符来编写 where 子句:

$users = DB::table('users')
                ->where('votes', '>=', 100)
                ->get();
$users = DB::table('users')
                ->where('votes', '<>', 100)
                ->get();
$users = DB::table('users')
                ->where('name', 'like', 'T%')
                ->get();

你还可以传递条件数组到 where 函数中:

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

混合数组 where 形式,数组里面在嵌套一个数组也是可以的

$wheres   = [
    'name' => 'sakuraovq',
    ['status', '>=', 2],
    ['money', '>', 0, 'or']
];
// select * from `user` where (`name` = ? and `status` >= ? or `money` > ?)
$users    = User::where($wheres)->get();

Or 语句

你可以一起链式调用 where 约束,也可以在查询中添加 or 字句。 orWhere 方法和 where 方法接收的参数一样:

$users = DB::table('user')
                    ->where('money', '>', 100, 'or')
                    ->orWhere('name', 'John')
                    ->get();

参数分组

有时候你需要创建更高级的 where 子句,例如「where exists」或者嵌套的参数分组。 Swoft 的查询构造器也能够处理这些。下面,让我们看一个在括号中进行分组约束的例子:

DB::table('user')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();

你可以看到,通过一个 Closure 写入where 方法构建一个查询构造器 来约束一个分组。这个 Closure 接收一个查询实例,你可以使用这个实例来设置应该包含的约束。上面的例子将生成以下 SQL:

select * from `user` where `name` = 'sakura' and (`money` > 100 or `title` = 'test')

WhereExists

whereExists 方法允许你使用 where exists SQL 语句。 whereExists 方法接收一个 Closure 参数,该 whereExists 方法接受一个 Closure 参数,该闭包获取一个查询构建器实例从而允许你定义放置在 exists 字句中查询:

DB::table('users')
            ->whereExists(function ($query) {
                $query->from('orders')
                      ->whereRaw('orders.user_id = users.id');
            })
            ->get();

上述查询将产生如下的 SQL 语句:

select * from `users`
where exists (
    select * from `orders` where `orders.user_id` = `users.id`
)

JsonWhere

Swoft 也支持查询 JSON 类型的字段(仅在对 JSON 类型支持的数据库上)。目前,本特性仅支持 MySQL 5.7+。

使用 -> 操作符查询 JSON 数据:

$users = DB::table('users')
                ->where('options->language', 'en')
                ->get();
$users = DB::table('users')
                ->where('preferences->dining->meal', 'cookie')
                ->get();

你也可以使用 whereJsonContains 来查询 JSON 数组:

$users = DB::table('users')
                ->whereJsonContains('options->languages', 'en')
                ->get();

MySQL 的 whereJsonContains 可以支持多个值:

$users = DB::table('users')
                ->whereJsonContains('options->languages', ['en', 'de'])
                ->get();

条件语句(when)

有时候你可能想要子句只适用于某个情况为真是才执行查询。例如你可能只想给定值在请求中存在的情况下才应用 where 语句。 你可以通过使用 when 方法:

$role = true;
$users = DB::table('users')
                ->when($role, function ($query, $role) {
                    return $query->where('role_id', $role);
                })
                ->get();

when 方法只有在第一个参数为 true 的时候才执行给的的闭包。如果第一个参数为 false ,那么这个闭包将不会被执行

你可以传递另一个闭包作为 when 方法的第三个参数。 该闭包会在第一个参数为 false 的情况下执行。为了说明如何使用这个特性,我们来配置一个查询的默认排序:

$sortBy = null;
$users = DB::table('users')
                ->when($sortBy, function ($query, $sortBy) {
                    return $query->orderBy($sortBy);
                }, function ($query) {
                    return $query->orderBy('name');
                })
                ->get();

插入(insert)

查询构造器还提供了 insert 方法用于插入记录到数据库中。 insert 方法接收数组形式的字段名和字段值进行插入操作:

DB::table('users')->insert(
    ['email' => 'john@example.com', 'votes' => 0]
);

你甚至可以将数组传递给 insert 方法,将多个记录插入到表中

DB::table('users')->insert([
    ['email' => 'taylor@example.com', 'votes' => 0],
    ['email' => 'dayle@example.com', 'votes' => 0]
]);
自增ID

如果数据表有自增 ID ,使用 insertGetId 方法来插入记录并返回 ID 值

$id = DB::table('user')->insertGetId([
    'age'  => 18,
    'name' => 'Sakura',
]);

更新(update)

查询构造器也可以通过 update 方法更新已有的记录。 update 方法和 insert 方法一样,接受包含要更新的字段及值的数组。你可以通过 where 子句对 update 查询进行约束:

更新 JSON 字段

更新 JSON 字段时,你可以使用 -> 语法访问 JSON 对象中相应的值,此操作只能用于支持 JSON 字段类型的数据库:

DB::table('users')
            ->where('id', 1)
            ->update(['options->enabled' => true]);

自增与自减

查询构造器还为给定字段的递增或递减提供了方便的方法。此方法提供了一个比手动编写 update 语句更具表达力且更精练的接口。

这两种方法都至少接收一个参数:需要修改的列。第二个参数是可选的,用于控制列递增或递减的量:

DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);

你也可以在操作过程中指定要更新的字段:

DB::table('users')->where('id', 1)->increment('votes', 1, ['updated' => 1]);

如果你想自定义更新 可以这样:

$res = DB::table('user')->where('id', $id)->update([
            'posts' => DB::raw('`posts` + 1'),
            'views' => Expression::new('`views` + 1'),
            'name'  => 'updated',
       ]);
// DB::raw(xxx) 等同 Expression::new(xxx) 使用这两个方法的时候要预防SQL注入

删除(delete)

查询构造器也可以使用 delete 方法从表中删除记录。 在使用 delete 前,可以添加 where 子句来约束 delete 语法:

DB::table('users')->where('votes', '>', 100)->delete();

如果你需要清空表,你可以使用 truncate 方法,它将删除所有行,并重置自增 ID 为零:

DB::table('users')->truncate();

查询构造器也包含一些可以帮助你在 select 语法上实现「悲观锁定」的函数。若向在查询中实现一个「共享锁」, 你可以使用 读锁 sharedLock 方法。 共享锁可防止选中的数据列被篡改,直到事务被提交为止 :

DB::table('users')->where('votes', '>', 100)->sharedLock()->get();

或者,你可以使用 写锁 lockForUpdate 方法。使用 「update」锁可避免行被其它共享锁修改或选取:

DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();

选择连接池

如果有多个连接连接池 ,默认分配的连接是在 db.pool默认连接池 里面获取的,如果要获取自己连接池的连接:

// 在 bean 里面配置的连接池名
$poolName = 'pool.order2';
$user = DB::query($poolName)->from('user')->where('id', $id)->get();

DB::query($poolName)方法获得同样是一个 Builder 对象

底层只有在 执行 sql 的时候才会从 DB 连接池中拿连接执行,执行之后会自动释放。Builder 对象不在依赖 Connection

释放连接: 把连接还给连接池

使用参数绑定防止注入

// 参数绑定,通过各种 Raw 方法
$name = 'admin';
$desc = '描述';
$builder = Builder::new()
    ->select('id', 'name', 'desc', 'salt', 'status', 'create_time', 'update_time')
    ->whereRaw('name=:name', [':name' => $name])
    ->whereRaw('desc like :desc', [':desc' => $desc. '%']) // 注意 like 的替换方式
    ->orderByDesc('create_time')
    ->forPage($page, $size);

PHP Swoft2 开源框架系列教程专栏推荐

Swoft2 框架精华教程:Validator 校验器详解

Swoft 框架精华教程:Devtool 详解

Swoft2 框架精华教程:Controller 组件解析,使用说明

Swoft2 框架精华教程:Config 配置解析,使用说明

Swoft2 框架精华教程:CLog 使用篇

Swoft2 框架精华教程:数据库 Migration

Swoft2 框架精华教程:数据库操作

Swoft2 框架精华教程: Swoft 组件开发单元测试

Swoft2 框架精华教程:面向切面编程(Aspect)

Swoft2 框架精华教程:Annotation 注解机制详解


网站公告

今日签到

点亮在社区的每一天
去签到