2

我有以下模型类,我的所有模型都对其进行了扩展。

class Model {
    [...]
    protected static $_query; // Query preparated

    public function prepare($query = null) {
        [...] // Connect to PDO, bla bla bla

        self::$_query = self::$link->prepare($query);
    }

    [...]

}

class Login extends Model {
    public function getUser($username = null) {
        self::prepare('SELECT * FROM usuarios WHERE usuario = :username LIMIT 1');
        self::bindValue('username', $username);

        return self::fetch();
    }
}

问题是,我需要在我的 mysql 中插入前缀,以避免表冲突,但不想编辑我的所有查询。

clientone_tablename
clienttwo_tablename
clientthree_tablename

准备查询时如何执行此操作、解析和插入表前缀?

我什么都没试过,因为我所知道的是,将我的自定义 PDO 扩展到 PHP PDO 类,这现在并不多..

我已经看到了: PDO - 使用表前缀。但不要正常工作..

谢谢!

4

2 回答 2

1

只需重写您的查询以使用在某处变量中找到的表前缀。解析所有表名查询比它的价值更麻烦。(你真的想写一个 SQL 解析器吗?)

于 2012-04-19T19:06:52.430 回答
1

所以我假设您只有 1 个 MySQL 数据库(您的虚拟主机上的最小软件包)并且需要为您的每个客户存储一个系统副本。

我的建议是,您创建一组单独的表(对于每个客户端),但名称无关紧要,因为您可以在客户端表中查找表名。

这是我给你的例子:clients表应该存储他们自己表的表名
(例如users_tbl = clientone_users for client id:1),以便稍后你可以查询clients表并获取他/她的表名,然后使用查询他/她的用户、新闻、页面和文件表的结果。

# SQL: new table structure
-- store the names of the clients tables here
CREATE TABLE clients(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    name VARCHAR(50),
    address VARCHAR(250),
    email VARCHAR(50),
    pass BLOB,
    /* table names*/
    users_tbl VARCHAR(70),
    news_tbl VARCHAR(70),
    pages_tbl VARCHAR(70),
    files_tbl VARCHAR(70)
) ENGINE = InnoDB;

# PHP: Some definitions for the table structure
$tbl_names = array("_users","_news","_pages","_files");
$tbl_fields = array();
$tbl_fields[0] = array("id INT","users_col1 VARCHAR(10)","users_col2 VARCHAR(20)");
$tbl_fields[1] = array("id INT","news_col1 DATE",...);
$tbl_fields[2] = array(...);
$tbl_fields[3] = array(...);
// refers to YOUR clients table field names (see above)
$clients_fields = array("users_tbl", "news_tbl", "pages_tbl", "files_tbl");

# PHP: Create a user and create the users database
function createUser($name, $address, $email, $pass, $salt) {
    global $db, $tbl_names, $tbl_fields;
    $success = false;
    if ($db->beginTransaction()) {
        $sql = "INSERT INTO clients(name, address, email, pass)
                     VALUES (?, ?, ?, AES_ENCRYPT(?, ?));"
        $query = $db->prepare($sql);
        $query->execute(array($name, $address, $email, $pass, $salt));
        if ($query->rowCount() == 1) { # if rowCount() doesn't work
            # get the client ID        # there are alternative ways
            $client_id = $db->lastInsertId();
            for ($i=0; $i<sizeof($tbl_names); $i++) {
                $client_tbl_name = $name . $tbl_names[$i];
                $sql = "CREATE TABLE " . $client_tbl_name . "("
                           . implode(',', $tbl_fields[$i]) . ");";
                if (!$db->query($sql)) {
                    $db->rollBack();
                    return false;
                } else {
                    $sql = "UPDATE clients SET ".clients_fields[$i]."=? "
                                       ."WHERE id=?;";
                    $query = $db->prepare($sql);
                    if (!$query->execute(
                            array($client_tbl_name, (int)$client_id)
                                         )) {
                        $db->rollBack();
                        return false;
                    }
                }
            }
            $db->commit();
            $success = true;
        }
        if (!$success) $db->rollBack();
    }
    return $success;
}

# PHP: Get the Client's table names
function getClientsTableNames($client_id) {
    $sql = "SELECT (users_tbl, news_tbl, pages_tbl, files_tbl)
              FROM clients WHERE id=?;";
    $query = $db->prepare($sql);
    if ($query->execute(array((int)$client_id)))
        return $query->fetchAll();
    else
        return null;
}

# PHP: Use the table name to query it
function getClientsTable($client_id, $table_no) {
    $table_names = getClientsTableNames($client_id);
    if ($table_names != null && isset($table_names[$table_no])) {
        $sql = "SELECT * FROM ".$table_names[$table_no].";";
        $query = $db->prepare($sql);
        if ($query->execute(array((int)$client_id)))
            return $query->fetchAll();
    }
    return null;
}
于 2012-04-19T20:38:23.210 回答