Php Doctrine-Symfony3:varchar主键未添加到数据库
我有我的用户实体设置,电子邮件(varchar)作为主键(id) 我做了一份登记表,一切正常 问题是当我在数据库中提交表单时,电子邮件列是空的 用户实体:Php Doctrine-Symfony3:varchar主键未添加到数据库,php,doctrine-orm,symfony,Php,Doctrine Orm,Symfony,我有我的用户实体设置,电子邮件(varchar)作为主键(id) 我做了一份登记表,一切正常 问题是当我在数据库中提交表单时,电子邮件列是空的 用户实体: /** * Utilisateur * * @ORM\Table(name="utilisateur", indexes={@ORM\Index(name="FK_utilisateur_niveau", columns={"niveau"})}) * @ORM\Entity */ class Utilisateur { /
/**
* Utilisateur
*
* @ORM\Table(name="utilisateur", indexes={@ORM\Index(name="FK_utilisateur_niveau", columns={"niveau"})})
* @ORM\Entity
*/
class Utilisateur
{
/**
* @var string
*
* @ORM\Column(name="pseudo", type="string", length=150, nullable=false)
*/
private $pseudo;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=250, nullable=false)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=150, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $email;
/**
* @var \EncheresBundle\Entity\Role
*
* @ORM\ManyToOne(targetEntity="EncheresBundle\Entity\Role")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="niveau", referencedColumnName="niveau")
* })
*/
private $niveau;
/**
* Set pseudo
*
* @param string $pseudo
*
* @return Utilisateur
*/
public function setPseudo($pseudo)
{
$this->pseudo = $pseudo;
return $this;
}
/**
* Get pseudo
*
* @return string
*/
public function getPseudo()
{
return $this->pseudo;
}
/**
* Set password
*
* @param string $password
*
* @return Utilisateur
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email
*
* @param string $email
*
* @return Utilisateur
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Set niveau
*
* @param \EncheresBundle\Entity\Role $niveau
*
* @return Utilisateur
*/
public function setNiveau(\EncheresBundle\Entity\Role $niveau = null)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return \EncheresBundle\Entity\Role
*/
public function getNiveau()
{
return $this->niveau;
}
}
以下是插入块:
if ($form->isSubmitted() && $form->isValid()) {
$obj->setEmail($form['email']->getData());
$obj->setPseudo($form['pseudo']->getData());
$obj->setPassword($form['password']->getData());
$obj->setNiveau($form['niveau']->getData());
$em = $this->getDoctrine()->getManager();
$em->persist($obj);
$em->flush();
$this->addFlash('notice', 'Inscription effectuee !');
return $this->redirectToRoute('login');
}
我哪里出错了?将类更改为具有唯一Id是否足够容易? 我在网上浏览了一下,我认为条令Id应该是一个整数(您使用的是什么数据库?) 因此,您可以修改代码并添加ID,如下所示:
class Utilisateur
{
/**
* @ORM\Id
* @ORM\Column(name="util_id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $util_id;
...
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=150, nullable=false)
*/
private $email;
...
我认为这将是一个快速解决方案,而且会很好地工作。所以经过长时间的搜索,我找到了解决方案 我编辑了user.orm.xml文件
<id name="email" type="string" column="email" length="150">
<generator strategy="IDENTITY"/>
</id>
到
删除使用实体并重新生成后:)能否显示完整的实体代码?到目前为止,从您发布的内容来看,一切似乎都正常。ok review i edit Question为什么要将表单值重新分配给您的实体<代码>$obj->setEmail($form['email']->getData())如果您以前绑定了表单和模型,则应自动分配它们:
$form=$this->createForm(TaskType::class,$task)$表单->HandlerRequest($request)代码>确定我将其替换为$obj=$form->getData(),但我仍然有同样的问题,并在die()函数中对其进行了测试,它获取表单的所有输入值,但当它插入数据库电子邮件时为空:(
<id name="email" type="string" column="email" length="150">
</id>