fliim
fliim

Reputation: 2189

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

Upvotes: 17

Views: 52069

Answers (5)

Muchammad Asrofie
Muchammad Asrofie

Reputation: 9

You can create fake object using $em->getReference(EntityName::class, "AnyValue");

Happy coding.

Upvotes: 0

Alvin Bunk
Alvin Bunk

Reputation: 7764

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.

Upvotes: 4

Dan Costinel
Dan Costinel

Reputation: 1736

I just replicated your case, and I managed to successfully save into the db 1 for true, and 0 for false.

Example:

//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');
}

I hope I did understood well your problem.

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

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

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

Let us know the result.

Upvotes: 2

Joe
Joe

Reputation: 2436

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.

Upvotes: 25

Related Questions