EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.07k stars 1.02k forks source link

Current date in 'date' field #2043

Closed alyamovsky closed 6 years ago

alyamovsky commented 6 years ago

Hi there!

I'm using Easyadmin with Symfony4. One of my entities has the field 'created_at' which goes like:

/**
* @ORM\Column(type="datetime", name="created_at")
*/
private $created;

and also I have the following config line describing this field in the new entity creation page:

fields:
...
- { property: 'created', type: 'date' }

Because sometimes I need to set it manually but most of the time the current date would be appropriate. But unfortunately this field is equal to 01-01-2013 by default so I need to set it manually every time. Is there any way to put current date value to it?

javiereguiluz commented 6 years ago

@ddlzz there's not an option for that ... but you could solve it by adding this to the constructor of your entity (as seen on https://stackoverflow.com/a/8714179/2804294):

class YourEntity
{
    /**
     * @var \DateTime
     */
    private $created;

    public function __construct()
    {
        $this-> created = new \DateTime();
    }
}
alyamovsky commented 6 years ago

@javiereguiluz thanks!