Doctrine: Set primary Key

Viewed 12160

I have a table with some fields and the first is my primary key, called token.

I need that token isn't automatically, so, I want to set this value. For example,

$em = $this->getDoctrine()->getManager();
$object->setToken("first");
$object->setValue("123");
$em->persist($object);
$em->flush();

But, in my DB, always token is null, why?

When I do flush, token value disappear.

In my entity, token is declared:

    /**
     * @var string
     *
     * @ORM\Column(name="token", type="string", length=45, nullable=false)
     * @ORM\Id
     */
    private $token;

    /**
     * Set token
     *
     * @param string $token
     * @return Downloads
     */
    public function setToken($token)
    {
        $this->token = $token;
    
        return $this;
    }

    /**
     * Get token
     *
     * @return string 
     */
    public function getToken()
    {
        return $this->token;
    }
2 Answers

Try with ORM\GeneratedValue

/**
 * @var string
 *
 * @ORM\Column(name="token", type="string", length=45, nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $token;
Related