Doctrine 中的默认值 [英] Default value in Doctrine

查看:24
本文介绍了Doctrine 中的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Doctrine 2 中设置默认值?

How do I set a default value in Doctrine 2?

推荐答案

不可移植"支持数据库默认值.使用数据库默认值的唯一方法是通过 columnDefinition 映射属性,您可以在其中指定列的 SQL 片段(DEFAULT 原因包括)字段被映射到.

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.

您可以使用:

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

首选 PHP 级别的默认值,因为它们也可以在新创建和持久化的对象上正确可用(在持久化新对象以获取默认值后,Doctrine 不会返回到数据库).

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).

这篇关于Doctrine 中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆