PHP PDO为所有查询添加过滤器

时间:2021-12-26 14:47:21

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

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

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

租户=公司A.

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.

再来一次。没有ORM,我已经知道如何使用SQL Alquemy,Laravel的雄辩和学说。我现在想知道是否可以使用PDO。

1 个解决方案

#1


1  

Create a class extending PDO like

创建一个扩展PDO的类

class DB extends PDO {...

class DB扩展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()...

通常我会覆盖__construct()方法来获取任何文档中的数据库凭据并连接到数据库,不要忘记在这个方法的底部调用父构造函数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...

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

#1


1  

Create a class extending PDO like

创建一个扩展PDO的类

class DB extends PDO {...

class DB扩展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()...

通常我会覆盖__construct()方法来获取任何文档中的数据库凭据并连接到数据库,不要忘记在这个方法的底部调用父构造函数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...

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