Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set doctrine entity boolean field to 0 instead of null

Im trying to persist an doctrine entity with a boolean field where the values are 0 or 1.

When the property is set to true, it save it as '1' in database. But when its 'false' or '0', it save it as NULL on database.

How can I fix this to only save only as 1 or 0 ?

The annotation for the property I use is like following:

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

When I set nullable to false, I cant persist it because it still want to set to null.

Thanks

When I persist it, the field value is 0

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

error: Can't save null

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

Doesn"t work, it still save null in base

Info 1

The actually insert query is trying to insert 0. But I got this error "ORA-01400: cannot insert NULL into (\"MYBASE\".\"MYTABLE\".\"SUBSTITUTE\")"

Info 2

Same append with another entity

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

Persisting

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

Result

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

Info 3

Inserting manually works using oci or oci8 driver

sql> INSERT INTO TestEntity (test_entity_id, substitute) VALUES (13, 0)
[2017-04-06 11:21:15] 1 row affected in 62ms
like image 809
fliim Avatar asked Apr 05 '17 14:04

fliim


1 Answers

Just set the SQL Default to 0 (Edit: You need to update the schema after that change):

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

Also you could initialize the property by default:

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

Nullable shouldn't matter as long as the value is set to either true/false.

If you really set the property to false before saving and it still saves it as null in the DB then something else is going on.

like image 102
Joe Avatar answered Sep 19 '22 20:09

Joe