【发布时间】:2015-11-02 18:48:37
【问题描述】:
我在下面有一个代码并尝试更改此行:
$sth = $db->prepare(Utils::prepareSQLPrefix($sql, Config::get('DB_PREFIX')));
在我的 DatabaseFactory 类中扩展某种 $db->prepare 函数(替换查询前缀)。
$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);
}
}
我该怎么做?谢谢!
【问题讨论】:
-
您的代码似乎有效。你有什么问题或问题?
-
我想将 prepareSQLPrefix 移至 DatabaseFactory 类
-
您应该在 DatabaseFactory 类上使用 _construct,然后在 Utils 上使用 parent::construct。或使用 protected 并将 DatabaseFactory 扩展到子类
-
这里
$sth->execute(array('name' =>$extension->component_name));你应该使用:name而不是name
标签: php mysql sql database pdo