Default value in Doctrine(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 中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 中的默认值


基础教程推荐
- php中的foreach复选框POST 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01