我有以下代码,并试图改变这一行:扩展PDO准备方法,以取代查询前缀
$sth = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));
延长某种$ DB->在我DatabaseFactory做好上课的准备功能(如果要替换查询前缀) 。
$db = DatabaseFactory::getFactory()->getConnection();
$sql = ' SELECT
`id`, `manifest`
FROM
`#__extensions`
WHERE
`name` = :name
LIMIT 1';
$sth = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));
$sth->execute(array('name' =>$extension->component_name));
$result = $sth->fetch();
我DatabaseFactory类:
class DatabaseFactory {
private static $factory;
private $database;
public static function getFactory()
{
if (!self::$factory) {
self::$factory = new DatabaseFactory();
}
return self::$factory;
}
public function getConnection() {
if (!$this->database) {
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$this->database = new PDO(
Config::get('DB_TYPE') . ':host=' . Config::get('DB_HOST') . ';dbname=' .
Config::get('DB_NAME') . ';port=' . Config::get('DB_PORT') . ';charset=' . Config::get('DB_CHARSET'),
Config::get('DB_USER'), Config::get('DB_PASS'), $options
);
}
return $this->database;
}
}
我的Utils类:
class Utils {
public static function prepareSQLPrefix($sql, $prefix) {
return str_replace('#__', $prefix, $sql);
}
}
我怎么能这样做呢?谢谢!
你的代码似乎是有效的。你有什么问题或问题? – Alex
我想将prepareSQLPrefix移动到DatabaseFactory类 – user889349
,您应该在DatabaseFactory类上使用_contruct,然后在Utils上使用parent ::构造。或者使用protected和扩展DatabaseFactory到子类 –