1

可以通过更改 factory.yml 中的设置将用户会话存储在数据库中:

all:
  storage:
    class: sfMySQLSessionStorage
    param:
      db_table:    session              # Name of the table storing the sessions
      database:    propel               # Name of the database connection to use
      # Optional parameters
      db_id_col:   sess_id              # Name of the column storing the session id
      db_data_col: sess_data            # Name of the column storing the session data
      db_time_col: sess_time            # Name of the column storing the session timestamp

从给出的示例中,似乎表中的列仅限于几个预定义的列。这意味着无法存储 Symfony 未预料到的其他信息,例如 ip 地址。

是否可以扩展数据库架构以包含更多自定义列?

4

2 回答 2

2

您可以像这样创建自己的会话类

class myPDOSessionStorage extends sfPDOSessionStorage {
    /**
    * Extending session write function
    *
    * @param string $id Session identifier  
    * @param mixed $data Session's data
    */
    public function sessionWrite($id, $data){
        /*
          Handle your specific data, e.g. :
        */
        $mySession = new MySession();
        $mySession->setIp($_SERVER['REMOTE_IP']);
        // connect with org. session
        $mySession->setSessionId($id);
        // Save extended session - you for sure need to add logic in finding existing one's
        $mySession->save();

        // IMPORTANT Call Parent function to update original session data
        parent::sessionWrite($id, $data);
    }
}

在 factory.yml 中:

all:
  storage:
    class: myPDOSessionStorage
    db_table:    my_session # Name of the table storing the sessions
    database:    my_session # Database connection to use

这是一个基本示例,因此您可以在此处找到更多信息:

于 2011-09-06T17:22:29.933 回答
0

不直接;会话的结构非常有限,尽管您可以在会话中存储相当多的数据。Symfony 只是将会话数据作为 blob 存储在数据库中。如果您确实需要,可以尝试修改 sfMySQLSessionStorage 类以将 $_SERVER['REMOTE_IP'] 包含在会话表中。

于 2009-03-09T16:24:43.183 回答