Doctrine: Set primary Key

10,895

Try with ORM\GeneratedValue

/**
 * @var string
 *
 * @ORM\Column(name="token", type="string", length=45, nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $token;
Share:
10,895
user2794692
Author by

user2794692

Updated on June 17, 2022

Comments

  • user2794692
    user2794692 almost 2 years

    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;
        }
    
  • user2794692
    user2794692 over 10 years
    thanks, but this isn't the problem, I have annotation, only primari key is stored like a default value.