phpormdoctrine-orm

Default value in Doctrine


How do I set a default value in Doctrine 2?


Solution

  • Database default values are not "portably" supported. The only way to use database default values is through the columnDefinition mapping attribute where you specify the SQL snippet (DEFAULT cause inclusive) for the column the field is mapped to.

    You can use:

    <?php
    /**
     * @Entity
     */
    class myEntity {
        /**
         * @var string
         *
         * @Column(name="myColumn", type="string", length="50")
         */
        private $myColumn = 'myDefaultValue';
        ...
    }
    

    PHP-level default values are preferred as these are also properly available on newly created and persisted objects (Doctrine will not go back to the database after persisting a new object to get the default values).