Shadow Prince
Shadow Prince

Reputation: 198

Set entity field default value using YML

I need get default value in class file

private $example_field = 0;

When I use this

example_field:
    options:
        default: 0

I got default database value, but when I try to persist without setting "su" I got error (column 'su' cannot be null).

Anyway,

example_field:
    default: 0

Does nothing.

Upvotes: 9

Views: 22819

Answers (3)

Frederic
Frederic

Reputation: 221

this work for me:

fields:
    my_field:
        options:
            default: 5

Upvotes: 22

Steffen Roßkamp
Steffen Roßkamp

Reputation: 141

The correct definition for a 0 default value would be by using quotes:

example_field:
    options:
        default: '0'

Upvotes: 3

Lighthart
Lighthart

Reputation: 3676

Doctrine does not support the SQL 'DEFAULT' keyword:

http://docs.doctrine-project.org/en/2.1/reference/faq.html

To solve this problem, you need to set defaults in your entity's constructor. If you want the default private variable in the class file, make and use a public getter function.

You can also try to set the variable during persist in a controller.

Upvotes: 0

Related Questions