2

I have an entity with property:

/**
 * @var string The title attr
 *
 * @ORM\Column(type="string", length=255)
 */
protected $title = '';

And form type is:

    $builder
        ->add('title', 'text', array(
            'required' => false,
        ))

But when I get this empty value from form and try to save this entity to the DB I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null

Why is it null? I set this property to the empty string '', and not null.

How can I store this entity in the DB with empty string '' (and without @ORM\Column(type="string", length=255, nullable=true))?

1 Answer 1

2

Set default column value as empty string:

/**
 * @var string The title attr
 *
 * @ORM\Column(type="string", length=255, options={"default":""})
 */
protected $title = '';
Sign up to request clarification or add additional context in comments.

6 Comments

Unrelated to me, but really neat solution :)
I try to do this, but it doesn't work for me. I think this is because I get title value from form, I updated question, added form type.
@Victor So how about setting empty_data?
@bartek Work only when I use space: 'empty_data' => ' ',, but when I use empty string '' error is same
@ferodss No, it's Doctrine mapping
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.