问题
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