How to set default value for entity attribute with symfony 5

I want to set a default value for an attribute called "status" in my entity called "Book" :

<?php

namespace AppEntity;

/**
 * @ORMEntity(repositoryClass=BookingRepository::class)
 */
class Booking
{
    const STATUS = [
        0 => 'Pending',
        1 => 'Canceled',
        2 => 'Confirmed',
        3 => 'Approved',
    ];


    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    
    /**
     * @ORMColumn(type="string", length=255, options={"default": "Pending"})
     */
    private $status = self::STATUS[0];


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function getBookingStatus(): ?string
    {
        return self::STATUS[$this->status];
    }

    public function setStatus(string $status): self
    {
        $this->status = $status;

        return $this;
    }


 
}

I declared a STATUS array with key/value to define a choice or enum type and I want to set the default value of $status as Pending ! is what I did right or need to do something else ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *