2017-04-11 120 views
0

搜索的一些小時之後,我找不到什麼我做錯了,我在這裏的情況:Symfony的3認證[語義錯誤]

用戶實體:

/** 
* 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-> group的字段中有一個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"} [] 

所以我敢肯定,我有跟團場,但無法找出問題。

有人幫我嗎?謝謝。

+0

首先,它似乎你有一個錯字:'$帳戶 - > getCreatedby($用戶);' 我認爲這是'$ account-> SetCreatedby($ user);' – scoolnico

+0

謝謝scoolnico。是的,對此感到抱歉。更正並仍然這樣做。 – Carlos

+0

您是否試圖檢查您的模式是否有效? 'bin/console doctrine:schema:validate'? – scoolnico

回答

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; 
+0

感謝tftd,但當我做出你的改變,我得到這個在生成實體: [Doctrine \ Common \ Annotations \ AnnotationException] [創建錯誤]註釋@ORM \ JoinColumn屬性AppBun dle \ Entity \ User: :$ group沒有名爲「cascade」的屬性。可用 屬性:name,referencedColumnName,unique,nullable,onDelete,列 定義,fieldName – Carlos

+0

@Carlos對不起,我犯了一個錯字。 'cascade'部分應該在你的'@ORM \ ManyToOne'中。請檢查更新後的答案。 – tftd

+0

感謝大家,但沒有任何工作。只有當我將關聯轉換爲ManyToMay時,它纔開始工作。與賬戶和用戶的關係相同。我很困惑。 – Carlos