Mở rộng lớp PDO có lẽ là lựa chọn tốt nhất.
class MyPDO extends PDO
{
protected $_table_prefix;
protected $_table_suffix;
public function __construct($dsn, $user = null, $password = null, $driver_options = array(), $prefix = null, $suffix = null)
{
$this->_table_prefix = $prefix;
$this->_table_suffix = $suffix;
parent::__construct($dsn, $user, $password, $driver_options);
}
public function exec($statement)
{
$statement = $this->_tablePrefixSuffix($statement);
return parent::exec($statement);
}
public function prepare($statement, $driver_options = array())
{
$statement = $this->_tablePrefixSuffix($statement);
return parent::prepare($statement, $driver_options);
}
public function query($statement)
{
$statement = $this->_tablePrefixSuffix($statement);
$args = func_get_args();
if (count($args) > 1) {
return call_user_func_array(array($this, 'parent::query'), $args);
} else {
return parent::query($statement);
}
}
protected function _tablePrefixSuffix($statement)
{
return sprintf($statement, $this->_table_prefix, $this->_table_suffix);
}
}
Nguồn
2009-09-24 16:06:44
hi là nó chỉ cho tôi hay về chức năng/phương pháp '_tablePrefixSuffix' bit cuối cùng '$ this_table_suffix' là nó giả sử là' $ this -> _ table_suffix'? – Val
Không chắc chắn làm thế nào điều này có thể được coi là ví dụ làm việc, vì 'sprintf' dự kiến định dạng chuỗi với các neo đặc biệt, nhưng đối số' $ statement' chỉ là một chuỗi đơn giản, mà chỉ đơn giản bị bỏ qua và trả về. –
Ok, đã tìm ra. Bạn chỉ cần chỉ định tên bảng với các neo: '$ table ="% 1 \ $ s {$ table}% 2 \ $ s "' –