Tp5框架中的where条件的使用

在我们一般使用这个框架中,where条件是依数组的形式来呈现的。比如说:

->where(['field'=>select])

但是,如果是数组的形式,无法满足我们的whereOR,因为数组在where条件中会转换成whereAnd。

这个时候,我们就可以使用where中的字符串形式。

->where('a=1 AND b=2 or c=3')

这样是不是很方便呢?看下图。

Db::table('think_user')->where('type=1 AND status=1')->select(); 

转成的sql是:

SELECT * FROM think_user WHERE type=1 AND status=1

为了安全,我们可以写成:

Db::table('think_user')->where("id=:id and username=:name")->bind(['id'=>[1,\PDO::PARAM_INT],'name'=>'thinkphp'])->select();

 

实现数据权限需要以下几个步骤: 1. 定义权限规则:定义哪些用户有权限访问哪些数据。可以根据角色、部门等进行权限划分。 2. 获取当前用户:需要获取当前登录的用户信息,以便进行权限判断。 3. 进行权限判断:根据当前用户的角色、部门等信息,判断该用户是否有权限访问当前数据。 4. 数据过滤:如果当前用户没有权限访问某些数据,则需要对这些数据进行过滤,确保用户无法访问。 下面是使用tp5框架实现数据权限的示例代码: 1. 定义权限规则 可以在config目录下新建auth.php文件,定义权限规则。 ```php <?php return [ 'role' => [ 'admin' => [ 'allow' => ['*'] ], 'user' => [ 'allow' => ['index', 'view'], 'deny' => ['create', 'edit', 'delete'] ] ] ]; ``` 以上定义了两个角色,admin和user。admin拥有所有权限,而user只能访问index和view两个操作,不能进行create、edit和delete操作。 2. 获取当前用户 可以在公共控制器Base.php,定义获取当前用户的方法。 ```php <?php namespace app\common\controller; use think\Controller; class Base extends Controller { protected $user = null; protected function initialize() { parent::initialize(); $this->user = session('user'); } } ``` 以上代码,通过session获取当前登录的用户信息。 3. 进行权限判断 可以在Base.php,定义一个checkAuth方法,用于进行权限判断。 ```php <?php namespace app\common\controller; use think\Controller; use think\facade\Config; use think\facade\Request; use think\exception\HttpException; class Base extends Controller { protected $user = null; protected function initialize() { parent::initialize(); $this->user = session('user'); } protected function checkAuth() { $rule = Config::get('auth.role.' . $this->user['role']); $action = Request::action(); if (in_array('*', $rule['allow']) || in_array($action, $rule['allow'])) { return true; } elseif (in_array($action, $rule['deny'])) { throw new HttpException(403, '您没有权限访问该页面'); } else { return false; } } } ``` 以上代码,首先获取当前用户的角色对应的权限规则。然后判断当前访问的操作是否在允许访问的操作列表或者在拒绝访问的操作列表。如果允许访问,则返回true;如果拒绝访问,则抛出一个异常;否则返回false。 4. 数据过滤 可以在模型,定义一个scopeAuth方法,用于对数据进行过滤。 ```php <?php namespace app\common\model; use think\Model; class User extends Model { protected $table = 'user'; public function scopeAuth($query) { $user = session('user'); $rule = Config::get('auth.role.' . $user['role']); if (in_array('*', $rule['allow'])) { return; } elseif (in_array('index', $rule['allow'])) { $query->where('status', 1); } else { $query->where('department_id', $user['department_id']); } } } ``` 以上代码,根据当前用户的角色对应的权限规则,对数据进行过滤。如果当前用户拥有所有权限,则不进行过滤;如果只能访问部分数据,则只返回状态为1的数据;否则返回该用户所在部门的数据。 最后,在控制器使用模型的scopeAuth方法,对数据进行过滤。 ```php <?php namespace app\index\controller; use app\common\model\User; class Index { public function index() { $list = User::auth()->select(); return view('index', ['list' => $list]); } } ``` 以上代码使用User::auth()方法,调用模型的scopeAuth方法,对数据进行过滤。最终只返回当前用户有权限访问的数据。 这样,就完成了tp5框架的数据权限实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩淼燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值