我尝试通过此代码注册新用户,并且我希望使用security.password_encoder编码密码,之后用户的persist密码属性具有值但我看到此错误
使用params [“root”执行'INSERT INTO用户(名称,系列,用户名,电子邮件,角色,密码,盐)VALUES(?,?,?,?,?,?,?)'时发生异常,“rooti”,“root”,“root @examl.com”,“[\”ROLE_ADMIN \“]”,null,“$ 2y $ 13 $ 70JDWmzFF0fuJyVCaB3 / ueISm3FgWRBMLAkSJqcQouNAh3qPnzcg。”]:
SQLSTATE [23000]:完整性约束违规:1048列'密码'不能为空
我的方法在这里:
// Currency: Convert GBP to EUR or Vice Versa
int main(){
double gbp_to_eur = 1.09;
double eur_to_gbp = 0.92;
char unit = '£';
double amount_to_convert = 0;
int AnyKey = 0;
cout << "Please enter the the unit you'd like to convert \n";
cin >> unit;
cout << "\n \n Now please enter the amount you'd like to convert. \n";
cin >> amount_to_convert;
switch (unit) {
case 'P':
cout << "Your " << unit << amount_to_convert << " is worth €" << amount_to_convert * gbp_to_eur << '.\n';
break;
case 'E':
cout << "Your " << unit << amount_to_convert << " is worth €" << amount_to_convert * eur_to_gbp << '.\n';
break;
default: cout << "The compiler isn't programmed for this unit of currency. \n";
break;
}
cin >> AnyKey;
}
这是我的用户实体
public function newAction(Request $request)
{
$user = new User();
$form = $this->createForm('AppBundle\Form\UserType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$encoder = $this->get("security.password_encoder");
$encoded = $encoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($encoded);
$user->setRoles(array('ROLE_ADMIN'));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_show', array('id' => $user->getId()));
}
return $this->render('user/new.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
我将安全算法设置为bcrypt是security.yml 在这里
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="family", type="string", length=255)
*/
private $family;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* @var array
*
* @ORM\Column(name="roles", type="json_array")
*/
private $roles;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $salt;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=10)
*/
private $password;
/**
* @var string
* @Assert\NotBlank()
* @Assert\Length(max=4096)
*/
private $plainPassword;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set family
*
* @param string $family
*
* @return User
*/
public function setFamily($family)
{
$this->family = $family;
return $this;
}
/**
* Get family
*
* @return string
*/
public function getFamily()
{
return $this->family;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set roles
*
* @param array $roles
*
* @return User
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($password)
{
$this->plainPassword = $password;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
$roles = $this->roles;
// guarantees that a user always has at least one role for security
if (empty($roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
*
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return;
}
public function eraseCredentials()
{
}
}
答案 0 :(得分:1)
您的列名在这里混淆:
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $salt;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=10)
*/
private $password;
您将salt
存储在名为password
的列中(必须在数据库中设置为不允许NULL,尽管注释中不存在该规则)和password
in一个名为salt
的列。只需翻转它们:
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=10)
*/
private $salt;