• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

232
Views
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
about 3 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 3 years ago · Santiago Trujillo Report

0

Change your driver from oci to oci8 in your paramters.yml file:

database_driver: oci8

That should do it. Use the Underground PHP and Oracle Manual for installing OCI8.

about 3 years ago · Santiago Trujillo Report

0

I think @Cerad's suggestion is correct, can you try:

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

Let us know the result.

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error