Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
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 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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 4 years ago · Santiago Trujillo Denunciar

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 4 years ago · Santiago Trujillo Denunciar

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 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda