Sven
Sven

Reputation: 13275

How to use empty strings instead of actual null values?

In my database schema, not all values have to be set, but instead of setting them to null, I would prefer to set them to an empty string.

How is this possible using Doctrine 2 in Zend Framework 2?

In the Doctrine 2 docs I found the annotation nullable that can be set to true, but in my case using a MySql database, this results in actual null values being written. Should I just set the default value to empty string?

Upvotes: 1

Views: 292

Answers (2)

KingCrunch
KingCrunch

Reputation: 131811

Although null is the representation of "nothing" and should be used for this: When you want the empty string to be the default you should set it as default.

class Foo
{
    private $bar = '';
    private $baz;
    public function __construct($baz = '')
    {
        $this->baz = $baz;
    }
}    

Upvotes: 1

DonL
DonL

Reputation: 123

You could always just set the fields in your model to an empty string when initializing the object. Just add the __construct method to your model and set the values there.

Upvotes: 0

Related Questions