首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >原理-Symfony3: varchar主键未添加到数据库

原理-Symfony3: varchar主键未添加到数据库
EN

Stack Overflow用户
提问于 2016-08-05 06:48:37
回答 2查看 946关注 0票数 1

我有我的用户实体设置,电子邮件(varchar)作为主键(id)我做了一个注册表,一切工作正常

问题是当我在数据库中提交表单时,电子邮件栏是空的

用户实体:

代码语言:javascript
复制
/**
 * 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;
    }
}

以下是插入块:

代码语言:javascript
复制
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');
        }

我哪里错了?

EN

回答 2

Stack Overflow用户

发布于 2016-08-05 17:36:58

因此,经过长时间的搜索,我找到了解决方案,我编辑了文件user.orm.xml

代码语言:javascript
复制
<id name="email" type="string" column="email" length="150">
   <generator strategy="IDENTITY"/>
</id>

代码语言:javascript
复制
<id name="email" type="string" column="email" length="150">
</id>

删除use实体并重新生成后:)

票数 1
EN

Stack Overflow用户

发布于 2016-08-05 07:15:05

将您的类更改为具有唯一Id是否足够简单?我在网上看了一下,我认为Doctrine Id需要是一个整数(您使用的是哪个数据库?)。

因此,您可以修改代码并添加ID,如下所示:

代码语言:javascript
复制
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;
    ...

我认为这将是一个快速的解决方案,并且会很好地工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38778567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档