PHP PDO添加过滤器到所有查询 [英] PHP PDO Add filter to all queries

查看:164
本文介绍了PHP PDO添加过滤器到所有查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,使用JUST PHP PDO(No ORMs)全局过滤所有的读取/插入/更新/删除操作。

一个例子是租户字段(在一个multitenant分贝)。我想全局地添加这种类型的过滤器到所有的操作:

tenant = companyA



再一次。没有ORM,我已经知道如何用SQL Alquemy,Laravel的雄辩和主义来做到这一点。我现在想知道是否可能只是用PDO。

解决方案

创建一个扩展 PDO like

class DB extends PDO {...



正常情况下,我重写 __ construct()方法来获取任何文档中的数据库证书并连接到数据库,不要忘记调用这个方法父构造函数 parent :: __ construct() ...

然后你可以使用你自己的方法 ($ params){
...
$ stmt = $ this->准备('')...

$ rows = $ stmt-> fetch(PDO :: FETCH_ASSOC)...
}

您可以在自己的方法中应用您的过滤器...

Is there a way, using JUST PHP PDO (No ORMs) to globally filter all fetch/insert/update/delete operations.

One example is a tenant field (in a multitenant db). I would like to globally add this type of filter to all operations:

tenant = companyA

Once again. NO ORMs, I already know how to do it with SQL Alquemy, Laravel's eloquent and Doctrine. I now want to know if it's possible just with PDO.

解决方案

Create a class extending PDO like

class DB extends PDO {...

Normally i override the __construct() method to get database credentials inside any document and connect to database, don't forget to call on bottom of this method the parent constructor parent::__construct()...

Then you can use your own methods

public function find($params) {
     ...
     $stmt = $this->prepare('')...

     $rows = $stmt->fetch(PDO::FETCH_ASSOC)...
}

Inside your own method you can apply your filters...

这篇关于PHP PDO添加过滤器到所有查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆