Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine: Set primary Key

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;
    }
like image 361
user2794692 Avatar asked Dec 19 '13 10:12

user2794692


1 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;
like image 102
Dev_NIX Avatar answered Oct 05 '22 23:10

Dev_NIX