关于身份验证的Symfony 3 [语义错误]

时间:2017-04-11 18:24:17

标签: symfony

经过几个小时的搜索,我找不到我做错了什么,我的情况就是这样:

用户实体:

/**
 * Many users belongs to one group
 *
 * @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
 * @ORM\JoinColumn(name="user_group", referencedColumnName="id")
 *
 */
private $group;

小组实体:

/**
 * One group has many users
 * 
 * @ORM\OneToMany(targetEntity="User", mappedBy="group")
 */
private $users;

public function __construct()
{
    $this->users = new ArrayCollection();
}

当访问者注册时,这是控制器中的方法:

/**
 *
 * @Route("/signup", name="signup")
 *
 */
public function signupAction(Request $request)
{

    $data = $request->request->all();
    $user = new User();
    $form = $this->createForm(SignupType::class, $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        // Create account
        $account = new Account();
        $account->getCreatedby($user);
        $account->setCreateddate(new \DateTime("now"));
        $account->setName($form['username']->getData());
        $account->setIsActive(true);

        $user->setAccount($account);    // relate this account to the user
        $user->setCreateddate(new \DateTime("now"));
        $user->setLocale('en'); // Assign the locale

        // Encoding the password
        $password = $this->get('security.password_encoder')
            ->encodePassword($user, $form['password']->getData());
        $user->setPassword($password);

        // Assign the group / role
        $group = $this->getDoctrine()->getRepository('AppBundle:Group')->findOneBy( array('name' => strtolower($form['user_type']->getData())) );
        //exit(\Doctrine\Common\Util\Debug::dump($group));
        $user->addGroup($group);

        // Persist objects to database
        $em = $this->getDoctrine()->getManager();
        $em->persist($account);
        $em->persist($user);
        $em->flush();

        return $this->render('default/show_message.html.twig', array(
            'alert_type' => 'success',
            'message' => 'You are registered',
            'redirect' => '',
        ));
    }

    return $this->render('security/signupForm.html.twig', array(
        'data' => $data,
        'form' => $form->createView(),
    ));

}

这几乎完美无缺。数据库已更新。但我注意到在user->组的字段中有一个null。当我取消注释转储时,我会看到正确的组数据。

因此,当访问者尝试登录时,我会收到此日志:

[2017-04-11 20:04:41] request.INFO: Matched route "login". {"route":"login","route_parameters":{"_controller":"AppBundle\\Controller\\SecurityController::loginAction","_route":"login"},"request_uri":"http://test.com/app_dev.php/login","method":"POST"} []
[2017-04-11 20:04:41] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationServiceException(code: 0): [Semantical Error] line 0, col 60 near 'g WHERE u.username': Error: Class AppBundle\\Entity\\User has no association named groups at C:\\xampp\\htdocs\\test_com\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Provider\\DaoAuthenticationProvider.php:94, Doctrine\\ORM\\Query\\QueryException(code: 0): [Semantical Error] line 0, col 60 near 'g WHERE u.username': Error: Class AppBundle\\Entity\\User has no association named groups at C:\\xampp\\htdocs\\test_com\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\Query\\QueryException.php:63, Doctrine\\ORM\\Query\\QueryException(code: 0): SELECT u, g FROM AppBundle\\Entity\\User u LEFT JOIN u.groups g WHERE u.username = :username OR u.email = :email at C:\\xampp\\htdocs\\test_com\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\Query\\QueryException.php:41)"} []
[2017-04-11 20:04:41] security.DEBUG: Authentication failure, redirect triggered. {"failure_path":"login_failure"} []

所以我确定我的群组字段存在问题,但无法找到。

有人帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

User实体类中,更改

/**
 * Many users belongs to one group
 *
 * @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
 * @ORM\JoinColumn(name="user_group", referencedColumnName="id")
 *
 */
private $group;

/**
 * Many users belongs to one group
 *
 * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", cascade={"persist"})
 * @ORM\JoinColumn(name="user_group", referencedColumnName="id")
 *
 */
private $group;