简体   繁体   中英

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

Try with ORM\\GeneratedValue

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

Maybe this could help Doctrine2 Primary Key .

And if you did not create your table using the doctrine command check if $token set to Primary Key in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM