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

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

问题描述

如何在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).

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

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