如何在原则2中设置默认值?


当前回答

你也可以使用xml:

<field name="acmeOne" type="string" column="acmeOne" length="36">
    <options>
        <option name="comment">Your SQL field comment goes here.</option>
        <option name="default">Default Value</option>
    </options>
</field>

其他回答

工作对我的mysql数据库也:

Entity\Entity_name:
    type: entity
    table: table_name
    fields: 
        field_name:
            type: integer
            nullable: true
            options:
                default: 1

你也可以使用xml:

<field name="acmeOne" type="string" column="acmeOne" length="36">
    <options>
        <option name="comment">Your SQL field comment goes here.</option>
        <option name="default">Default Value</option>
    </options>
</field>

数据库默认值不支持“可移植”。使用数据库默认值的唯一方法是通过columnDefinition映射属性,在该属性中为字段映射到的列指定SQL片段(包括default原因)。

你可以使用:

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

php级别的默认值是首选的,因为这些值在新创建的和持久化的对象上也是正确可用的(在持久化一个新对象后,Doctrine不会返回数据库以获得默认值)。

在实体中设置一个构造函数,并在那里设置默认值。

如果对实体使用yaml定义, 以下工作为我在postgresql数据库:

Entity\Entity_name:
    type: entity
    table: table_name
    fields: 
        field_name:
            type: boolean
            nullable: false
            options:
                default: false