yii\web\DbSession::writeSession PHP Method

writeSession() public method

Do not call this method directly.
public writeSession ( string $id, string $data ) : boolean
$id string session ID
$data string session data
return boolean whether session write is successful
    public function writeSession($id, $data)
    {
        // exception must be caught in session write handler
        // http://us.php.net/manual/en/function.session-set-save-handler.php#refsect1-function.session-set-save-handler-notes
        try {
            $query = new Query();
            $exists = $query->select(['id'])->from($this->sessionTable)->where(['id' => $id])->createCommand($this->db)->queryScalar();
            $fields = $this->composeFields($id, $data);
            if ($exists === false) {
                $this->db->createCommand()->insert($this->sessionTable, $fields)->execute();
            } else {
                unset($fields['id']);
                $this->db->createCommand()->update($this->sessionTable, $fields, ['id' => $id])->execute();
            }
        } catch (\Exception $e) {
            $exception = ErrorHandler::convertExceptionToString($e);
            // its too late to use Yii logging here
            error_log($exception);
            if (YII_DEBUG) {
                echo $exception;
            }
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * Session write handler.
  * Do not call this method directly.
  * @param string $id session ID
  * @param string $data session data
  * @return boolean whether session write is successful
  */
 public function writeSession($id, $data)
 {
     if ($this->dbSession) {
         return $this->dbSession->writeSession($id, $data);
     } else {
         parent::writeSession($id, $data);
     }
 }
All Usage Examples Of yii\web\DbSession::writeSession