1

I can't set false value to an entity column which type is boolean.

/**
 * @ORM\Column(type="boolean")
 */
private $isActive;

Sending JSON:

{myEntity: {isActive: false}}

...will cause:

Integrity constraint violation: 1048 Column 'is_active' cannot be null

While sending:

{myEntity: {isActive: 0}}

...will work fine

There are some similar answers on stackoverflow however none of the solution worked.

RAW COLUMN

+--------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra           |
+-------------+--------------+------+-----+---------+-----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment  |
| is_active   | tinyint(1)   | NO   |     | NULL    |                 |
+-------------+--------------+------+-----+---------+-----------------+

EDIT

Read my answer below. It's not a Doctrine bug but MariaDB 10.2 serie.

undefinedman
  • 620
  • 1
  • 11
  • 25
  • 1
    Please show the Controller and FormType processing the call. Along with the entity setter. However I suggest specifying a default value if null is not allowed. – Will B. Apr 25 '18 at 17:06
  • The problem does not lay in the Controllers or FormTypes. The problem lies in Doctrine itself or DBAL. – undefinedman Apr 26 '18 at 10:35

2 Answers2

1

This is unfortunately not a solution to the main problem, however it will give some insight.

The problem translating false to null occurs with MariaDB 10.2 serie (did not test 10.0 serie).

The described problem does not occur on MySQL or MariaDB 5.5 serie and it transforms false to false in a correct way.

Going to make another issue specifically about MariaDB 10.2 bug.

undefinedman
  • 620
  • 1
  • 11
  • 25
-1

Alternatively, you should evaluate doing:

/**
 * @ORM\Column(type="boolean", nullable=true)
 */
private $isActive;

You will not always have the chance of and/or will want to initialize a value in the class.

UPDATE:

I realize that my answer wasn't the right one. Maybe you could initialize the property in the constructor, or call the setter automatically. In any case, you have to "deceive" Doctrine's Unit of Work that your entity has changed.

  • This doesn't answer the question, he might not want the bolean to be able to be null. – Liora Haydont Apr 25 '18 at 18:41
  • Yeap, he **might**, that's why is an alternative. In any case, I realized that the problem is because the entity is not updated for doctrine's Unit of Work. That's another issue. – Matías Navarro Carter Apr 25 '18 at 18:54
  • your solution will do work, because you explicitly said that isActive may be null and it will work, however I am still looking for a way to send just false and this must work – undefinedman Apr 26 '18 at 10:25