Add a column to an existing entity in Symfony

前端 未结 9 1905
误落风尘
误落风尘 2020-12-22 21:27

I\'ve been playing around with Symfony on my web server and I\'ve been creating entity with doctrine for my database. I wanted to add a column to one of these entity... I wa

9条回答
  •  渐次进展
    2020-12-22 21:35

    FOR SYMFONY3 USERS...

    here you have to follow two steps to make changes in your entity Step1: Open Your Entity File..For EX: "Acme\MyBundle\Entity\Book"

    Current Entity is having few fields like:id,name,title etc. Now if you want to add new field of "image" and to make change constraint of "title" field then add field of "image" with getter and setter

    /**
         * @var string
         *
         * @ORM\Column(name="image", type="string", length=500)
         */
        private $image;
    

    And add getter and setter

    /**
         * Set image
         *
         * @param string $image
         *
         * @return Book
         */
        public function setImage($image)
        {
            $this->image = $image;
    
            return $this;
        }
    
        /**
         * Get image
         *
         * @return string
         */
        public function getimage()
        {
            return $this->image;
        }
    

    To update existing field constraint of title from length 255 to 500

    /**
         * @var string
         *
         * @ORM\Column(name="title", type="string", length=500)
         */
        private $title;
    

    Ok...You have made changes according to your need,And You are one step away.

    Step 2: Fire this command in project directory

    php bin/console doctrine:schema:update --force
    

    Now,check your table in database ,It's Done!!

提交回复
热议问题