如何写一个属于自己的数据库封装(5)

基本思路

在开始代码之前, 我们需要回想一些日常面对的难题, 或则说不良体验

在实现业务逻辑时, 我们往往都会遇到以下类似场景

确认A先生(id=233)是否会员, 如果是, 修改字段’status’为’active’, 否则删除它

在没有框架的时候一般都会这么写的(以下代码略过所有框架逻辑)

// 首先查询A先生的数据, 获取他的身份  $a = 'select * from users where id = 233';  // 判定是否为会员  if($a->member === true)      // 是就修改字段     $b = 'update users set status = 'active' where id = 233';  else     // 否就删除数据     $b= 'delete from users where id = 233';

注意,这是因为我们简略了pdo的所有步骤, 试想想更复杂的业务逻辑, 曾经见过满满上千行都是SQL语句的, 代码可读性等于0, 重构无能
所以,凭什么我们需要写的这么多呢, 可以简化啊!

可再操作的数据

当数据返回后,将其转化为实例, 该实例附带框架, 可以进行某些操作

这就是查询篇中为什么数据返回后会被放入函数cast()当中进行转化的原因

使用封装后可以这么做

// 首先查询A先生的数据, 获取他的身份  $a = User::find(233);  // 判定是否存在该id和该id是否为会员  if($a & $a->member)      // 是就修改字段     $b = $a->update(['status'=>'active']);  else     // 否就删除数据     $b= $a->delete();

接下来我们需要思考

如何对数据库数据进行修改?

这是我自己的肤浅答案

常见的情况我们需要用到条件语法, 对需要修改的数据指定范围, 过滤无需改写的数据

根据以上的说明, 有三种例子

  1. 完全不指定范围, 框架内所有数据

    update Actor set first_name = 'new data'
  2. 指定范围, 修改匹配条件的多条数据

    update Actor set first_name = 'new data' where first_name like '%L%'
  3. 指定范围, 依据表的 identity 框架 或 unique key 修改指定单条数据

    update Actor set first_name = 'new data' where actor_id = 10

根据以上的三种类型, 我们可以开始开发update函数了,
PS:过于高级的update语句我因为经验尚浅无法理解/不曾使用, 欢迎留言


Builder.php

在最后一行添加update函数

// 改写数据库数据      public function update(array $values) {          // 如果写保护已经开启,跳出错误          if($this->writeLock) throw new Exception("data is not allow to update");            // 编译update语句          $sql = $this->grammar->compileUpdate($this, $values);            // 将所有变量的值合成一个数组, 其中包括条件语句部分          $bindings = array_values(array_merge($values, $this->getBindings()));            // 返回改写结果,成功true失败false          return $this->connector->update($sql, $bindings);      }

Grammar.php

在最后一行添加compileUpdate函数

    public function compileUpdate(Builder $query, $values) {          // 循环$values, 记得引用          foreach ($values as $key => &$value)               // 将所有$value改成对应的$key=?              $value = $key.' = ?';            // 将$values中的之全部掏出在连接起来          $columns = implode(', ', array_values($values));            // 附上where语句如果有          // 由于更复杂的sql update语句我还没试过, 为了不坑人, 所以限制只有where语法有效          // 欢迎提供更复杂的where语句          $where = is_null($query->wheres) ? '' : $this->compileWheres($query);            // 返回update语句          return trim("update $query->from set $columns $where");      }

框架.php

添加函数save, 用法见下面例子

// 一种更快捷的update方式      public function save() {          return $this->update((array)$this->data);      }

例子

  • 修改指定数据

$a = Actor::where('first_name', 'ANGELINA')      ->update(['last_name'=>'changed']);  dd($a);
  • 再操作数据实例

$a = Actor::where('first_name', 'ANGELINA')      ->first();  dd($a->update(['last_name'=>'again']));
  • 再操作数据实例, 另一种用法

$a = Actor::where('first_name', 'ANGELINA')      ->first();    $a->last_name = 'save';  dd($a->save());

返回结果

boolean true  // 失败返回false

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享