将doctrine entity boolean field设置为0而不是null

fli*_*iim 6 php doctrine symfony doctrine-orm

我试图用一个布尔字段来保持一个doctrine实体,其值为0或1.

当该属性设置为true时,它将其保存为数据库中的"1".但是当它的'false'或'0'时,它将它保存为数据库中的NULL.

如何解决此问题仅保存为1或0?

我使用的属性的注释如下:

@ORM\Column(name="substitute", type="boolean", nullable=true)
Run Code Online (Sandbox Code Playgroud)

当我将nullable设置为false时,我不能坚持它,因为它仍然想要设置为null.

谢谢

当我坚持它时,字段值为0

尝试1 @ORM\Column(name ="substitute",type ="boolean",options = {"default":"0"}))

错误:无法保存null

尝试2 @ORM\Column(name ="substitute",type ="boolean",nullable = true,options = {"default":"0"}))

不起作用,它仍然在基础上保存null

信息1

实际插入查询试图插入0.但是我收到此错误"ORA-01400:无法插入NULL(\"MYBASE \".\"MYTABLE \".\"SUBSTITUTE \")"

信息2

与另一个实体相同

class TestEntity
{
    /**
     * @ORM\Column(name="test_entity_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="substitute", type="boolean")
     */
    private $isSubstitute = false;
}
Run Code Online (Sandbox Code Playgroud)

坚持

$test = new TestEntity();
$test->setIsSubstitute(false);
$em->persist($test);
Run Code Online (Sandbox Code Playgroud)

结果

request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\NotNullConstraintViolationException: "An exception occurred while executing 'INSERT INTO TestEntity (test_entity_id, substitute) VALUES (?, ?)' with params [7, 0]: SQLSTATE[HY000]: General error: 1400 OCIStmtExecute: ORA-01400: cannot insert NULL into ("MYBASE"."TESTENTITY"."SUBSTITUTE")  (ext\pdo_oci\oci_statement.c:148)"\n (ext\\pdo_oci\\oci_statement.c:148) at PATH\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Driver\\PDOStatement.php:91)"} []
Run Code Online (Sandbox Code Playgroud)

信息3

手动插入使用oci或oci8驱动程序

sql> INSERT INTO TestEntity (test_entity_id, substitute) VALUES (13, 0)
[2017-04-06 11:21:15] 1 row affected in 62ms
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 8

只需将SQL Default设置为0(编辑:您需要在更改后更新架构):

/**
 * @ORM\Column(type="boolean", options={"default":"0"})
 */
protected $isActive;
Run Code Online (Sandbox Code Playgroud)

您也可以默认初始化该属性:

/**
 * @ORM\Column(type="boolean", options={"default":"0"})
 */
protected $isActive = false;
Run Code Online (Sandbox Code Playgroud)

只要该值设置为true/false,Nullable就不重要了.

如果你在保存之前确实将属性设置为false,并且它仍然在数据库中将其保存为null,那么还会发生其他事情.


Alv*_*unk 4

在 paramters.yml 文件中将oci驱动程序从 更改为:oci8

database_driver: oci8
Run Code Online (Sandbox Code Playgroud)

应该可以做到这一点。使用地下 PHP 和 Oracle 手册来安装 OCI8。