在自定义UserProvider中使用EntityManager

时间:2013-12-25 20:59:23

标签: php symfony doctrine

我正在尝试在Symfony2中的自定义用户提供程序中使用Doctrines Entity Manager。

如您所见,我将其作为论据传递:

SnapsavedUserProvider: 
    class: Snap\ModelBundle\Security\SnapsavedUserProvider
    arguments: ["@doctrine.orm.entity_manager"]

在课堂上我做了:

namespace Snap\ModelBundle\Security;

use Snap\RestBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class SnapsavedUserProvider implements UserProviderInterface {

    protected $em;

    public function __construct($em) {
        $this->setEm($em);
        /* IF i var dump $this-getEm() here, i get the EntityManager */
    }

    public function getEm() {
        return $this->em;
    }

    public function setEm($em) {
        $this->em = $em;
    }


    public function loadUserByUsername($username) {

        var_dump($this->getEm());
        /* But its NULL here? */
        die();

        $user = $this->getEm()
                ->getRepository('SnapRestBundle:User')
                ->findOneBy(array('username' => $username));

        if ($user) {
            return $user;
        }

        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
}

为什么我的EntityManager在loadUserByUsername方法中为NULL?我的意思是,即使使用了新实例,也会注入并设置em,不是吗?

1 个答案:

答案 0 :(得分:1)

为什么在通过构造函数注入时使用getter和setter?另外,您缺少界面所需的几种方法。

<?php

namespace Snap\ModelBundle\Security;

use Doctrine\ORM\EntityManager;
use Snap\RestBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class SnapsavedUserProvider implements UserProviderInterface
{

    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function loadUserByUsername($username)
    {

        $user = $this->em
            ->getRepository('SnapRestBundle:User')
            ->findOneBy(array('username' => $username));

        if ($user) {
            return $user;
        }

        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }

    /**
     * Refreshes the user for the account interface.
     *
     * It is up to the implementation to decide if the user data should be
     * totally reloaded (e.g. from the database), or if the UserInterface
     * object can just be merged into some internal array of users / identity
     * map.
     * @param UserInterface $user
     *
     * @return UserInterface
     *
     * @throws UnsupportedUserException if the account is not supported
     */
    public function refreshUser(UserInterface $user)
    {
        // TODO: Implement refreshUser() method.
    }

    /**
     * Whether this provider supports the given user class
     *
     * @param string $class
     *
     * @return Boolean
     */
    public function supportsClass($class)
    {
        // TODO: Implement supportsClass() method.
    }

}
相关问题