2014-01-28 114 views
-1

我會將會話數據存儲在數據庫中,但我不知道哪種類型的列最好使用(我將存儲序列化的對象和數組)。我認爲更好的方式是使用BYTEA的原因,當我嘗試前幾天存儲對象與命名空間的PHP的Pdo失去了一個頭(從命名空間斜線)。爲了解決問題,它將商店對象轉換爲BYTEA柱,並在綁定值時告訴php使用類型PDO :: PARAM_LOB。在數據庫中存儲會話數據更好bytea或文本類型列

$this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB); 

但是這個參數只對BYTEA列(Postgresql)有效。所以最好選擇BYTEA列類型而不是TEXT?是否有興趣知道什麼時候使用這種列類型?

我將不勝感激。

UPDATE:

我會使用的session_set_save_handler功能和SessionHandlerInterface實現我的會話處理系統。它看起來像這樣:

class SessionHandler{ 

    public function open($save_path, $session_name) {// some code} 

    public function close() {//some code} 

    public function read($id) {// some code} 

    public function write($id, $data) { 
    // Here i would implement mechanism to store object into database using php pdo. 
    // Here variable $data is already serialized by php session mechanism and ready to put into database. 
    // But i have unpleasantly experience storing serialized object 
    // using pdo client with namespaces thought bindParam function. 

    // This experience is: 
    // The serialized object (string) with namespace was cut at half, 
    // when i tried used PDO::PARAM_STR as argument in $sth->bindValue() (TEXT column i database). 
    // When to the same operation i used PDO::PARAM_LOB (BYTEA column in database) it was stored all fine. 
    } 

    public function destroy($id) {//some code} 

    public function gc($maxlifetime) {// some code} 

} 

$hnadler = new SessionHandler() 
session_set_save_handler($handler, true); 

並經過這種經驗im不知道哪個coulm使用。

UPDATE:

謝謝你的菜刀答案克雷格·林格:您的帖子後,我決定去看看究竟是寫PHP手冊中關於序列化對象:

http://www.php.net/manual/en/function.serialize.php

連載功能

Returns a string containing a byte-stream representation of value 
    that can be stored anywhere. 

    Note that this is a binary string which may include null bytes, 
    and needs to be stored and handled as such. For example, serialize() 
    output should generally be stored in a BLOB field in a database, 
    rather than a CHAR or TEXT field. 

因此,您建議將對象存儲在BYTEA類型列中,而不是TEXT。

+0

_slashes from namespace_是什麼意思?如果會話信息包含命名空間類並存儲它們,它們當然也會包含斜槓。 – DrColossos

回答

2

如果您要存儲序列化的應用程序數據,則bytea是合適的選擇。這只是字節,除了提供它的應用程序之外,對於其他任何內容都是沒有意義的,並且它不是文本,因爲它不是一個有意義的字符。

因此,對於數據建模原因,bytea是你應該使用的。

此外,如果數據可能包含空字節,則必須使用bytea,因爲text不能包含空字節,並且將數據編碼爲(例如)base64以解決此問題效率低下且可怕。

+0

謝謝你的回答 – ZiupeX