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

时间:2017-04-05 14:35:52

标签: php symfony doctrine-orm doctrine

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

当该属性设置为true时,将其保存为' 1'在数据库中。 但是,如果它的错误'或者' 0',它在数据库中将其保存为NULL。

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

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

@ORM\Column(name="substitute", type="boolean", nullable=true)

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

由于

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

尝试1 @ORM \ Column(名称="替换",类型="布尔",选项= {"默认":" 0"}))

错误:无法保存空

尝试2 @ORM \ Column(名称="替换",类型="布尔",nullable = true,选项= {"默认":" 0&#34 ;}))

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

信息1

实际插入查询试图插入0.但是我得到了这个错误" ORA-01400:无法插入NULL(\" MYBASE \"。\&#34 ; MYTABLE \" \"替代\")"

信息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;
}

迁延性

$test = new TestEntity();
$test->setIsSubstitute(false);
$em->persist($test);

结果

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)"} []

信息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

5 个答案:

答案 0 :(得分:8)

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

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

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

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

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

如果您在保存之前确实将该属性设置为false,并且仍然将其保存为数据库中的null,则会发生其他事情。

答案 1 :(得分:1)

我认为@ Cerad的建议是正确的,你可以尝试一下:

/**
 * @ORM\Column(name="substitute", type="boolean")
 */
protected $substitute = false;

让我们知道结果。

答案 2 :(得分:1)

将您的驱动程序从paramters.yml文件中的select case mod(to_number(to_char(sysdate, 'MM')), 3) when 0 then 3 when 1 then 1 when 2 then 2 end as month_in_quarter from dual; 更改为oci

oci8

应该这样做。使用the Underground PHP and Oracle Manual安装OCI8。

答案 3 :(得分:1)

您可以使用$em->getReference(EntityName::class, "AnyValue");

创建伪造的对象

快乐的编码。

答案 4 :(得分:0)

我刚刚复制了你的案例,我成功地保存到db 1中为true,0为false。

示例:

//Entity
Person: id, name, isMajor(boolean field)
//IMPORTANT: I setted the boolean field inside __construct() method, and let it be, by default, false (0). This means you don't need anymore to have that options={"default":"0"}.

//...
/**
 * @var bool
 *
 * @ORM\Column(name="isMajor", type="boolean", nullable=true)
 */
private $isMajor;

public function __construct() 
{
    $this->isMajor = false;
}

//Created CRUD against the Entity

//When saving (either using the default actions provided by the CRUD, or by setting the values inside another controller's action):
//AppBundle/Controller/DefaultController.php
/**
 * @Route("/new-person")
 */
public function createAction()
{
    $person = new Person();
    $person->setName('name');
    $person->setIsMajor(true); // this saves 1 in the table
    $person->setIsMajor(false); // this saves 0 in the table

    $em = $this->getDoctrine()->getManager();
    $em->persist($person);
    $em->flush();

    return $this->redirectToRoute('person_index');
}

我希望我能很好地理解你的问题。