簡體   English   中英

db + FOSUserbundle中的Symfony 2.8會話存儲

[英]Symfony 2.8 session storage in db + FOSUserbundle

我的會話有問題。 我嘗試使用PdoSessionHandler在數據庫中存儲會話,並需要與用戶連接會話。 所以我寫了一個Login Listener:

class LoginListener implements EventSubscriberInterface
{
    /**
    * @var EntityManager
    */
    protected $entityManager;

    /**
     * @var Session
     */
    protected $session;

    /**
     * LoginListener constructor.
     *
     * @param Session       $session
     * @param EntityManager $entityManager
     */
    public function __construct(Session $session, EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
        $this->session       = $session;
    }

    /**
     * @param InteractiveLoginEvent $event
     */
    public function onLogin(InteractiveLoginEvent $event)
    {
        $user      = $event->getAuthenticationToken()->getUser();
        if ($user) {
            $sessionId = $this->session->getId(); // hrj91gs3go3mlqat2hufrt54f6
            dump($sessionId);
            $sessions = $this
                ->entityManager
                ->getRepository('IntersynergyUserBundle:Sessions')
                ->findAll();
            dump($sessions); //hrj91gs3go3mlqat2hufrt54f6 this session id isn't in results

            $session = $this
                ->entityManager
                ->getRepository('IntersynergyUserBundle:Sessions')
                ->findOneBy(['id' => $sessionId]);
            dump($session); //empty results
            exit();
            if ($session) {
                $user->setSession($session);
                $this->entityManager->persist($user);
                $this->entityManager->flush();;
            }
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            FOSUserEvents::REGISTRATION_CONFIRM      => 'onLogin',
            FOSUserEvents::SECURITY_IMPLICIT_LOGIN   => 'onLogin',
            FOSUserEvents::RESETTING_RESET_COMPLETED => 'onLogin',
            FOSUserEvents::SECURITY_IMPLICIT_LOGIN   => 'onLogin',
            SecurityEvents::INTERACTIVE_LOGIN        => 'onLogin',
        ];
    }
 }

但是當用戶開始登錄時,當我嘗試從數據庫讀取會話時,我得到了空的主義結果。 這很奇怪,因為當我查看數據庫會話時,這個ID存在。

我認為getRepository('IntersynergyUserBundle:Sessions') - > findOneBy(['id'=> $ sessionId]); 轉儲($會議); //空結果

問題在於此處。 你可以在這里發布轉儲($ sessions)嗎? 你確定它是用Id鍵映射的嗎?

我找到了如何在獲取會話ID后解決這個問題我保存了會話。

/**
 * @param User $user
 */
private function setUserSession(User $user)
{
    $sessionId = $this->session->getId();
    //this fix issues with regenerating session id
    $this->session->save();
    $session
        = $this
        ->entityManager
        ->getRepository('IntersynergyUserBundle:Sessions')
        ->findOneBy(['id' => $sessionId]);

    if ($session) {
        $session->setUser($user);
        $this->entityManager->persist($session);
        $this->entityManager->flush();
    }
}

並修改用戶和會話實體:

用戶:

 userSessions:
        targetEntity: Intersynergy\UserBundle\Entity\Sessions
        mappedBy: user

會議:

/**
 * @var
 *
 * @ORM\ManyToOne(targetEntity="TwojTarotBundle\Entity\User", inversedBy="userSessions")
 */
private $user;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM