How to set a default value for an entity field in Doctrine?
Hi everyone! I'm just starting to learn Doctrine ORM in Symfony, and I'm having trouble setting a default value for one of the entity fields.
I have a `User` entity, and I want the `status` field to have a default value of `'active'`. Here's how I tried to do it:
```php
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User
{
/**
* @ORM\Column(type="string", length=20, options={"default": "active"})
*/
private $status;
}
```
But when I create a new user without specifying the status:
```php
$user = new User();
$em->persist($user);
$em->flush();
```
The `status` field remains `null` instead of `'active'`. I expected Doctrine to automatically set the default value when saving.
I've read the docum
No answers yet.
Log in to leave an answer
Blogs
Working with Dates and Time in Java: Advanced Techniques and API
TDD in JavaScript: How to Stop Fearing Bugs and Start Coding with Confidence
TypeScript from Zero to PRO: Best YouTube Channels and Courses in 2024
OAuth 2.0 and JWT Authorization in TypeScript: A Complete Guide for Beginners