PHP PDO Add filter to all queries

萝らか妹 提交于 2019-12-24 00:47:48

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/33219951/php-pdo-add-filter-to-all-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!