Souf
Souf

Reputation: 611

Doctrine-Symfony3 : varchar primary key not added to database

I have my user entity setup, and email (varchar) as primary key (id) i did a registration form and all is working fine

the problem is when i submit the form in database, the email column is empty

User entity :

/**
 * 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;
    }
}

here is the insert block:

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');
        }

where did i go wrong ?

Upvotes: 1

Views: 1027

Answers (2)

Souf
Souf

Reputation: 611

So after a long search i found solution i edited the file user.orm.xml

<id name="email" type="string" column="email" length="150">
   <generator strategy="IDENTITY"/>
</id>

to

<id name="email" type="string" column="email" length="150">
</id>

after delete the use entity and regenerate it :)

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

Is it easy enough to change your class to have a unique Id instead? I looked online briefly, and I think the Doctrine Id needs to be an integer (what database are you using?).

So you could modify your code and add an ID like so:

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;
    ...

I think that would be a quick fix and would work well.

Upvotes: 0

Related Questions