Всем доброе утро . Здесь я использую esayadmin v3, я бы хотел, чтобы, когда пользователь добавляет реализацию, и чтобы дата создания, созданная, не отображалась на странице и чтобы текущая дата помещалась в мою базу данных. Поскольку я сделал это, но пользователь может редактировать файл createdAt. вот реализация моей сущности
<?php
namespace App\Entity;
use App\Repository\RealisationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=RealisationRepository::class)
* @Vich\Uploadable
*/
class Realisation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type = "integer")
*/
private $id;
/**
* @ORM\Column(type = "string", length=255)
*/
private $nom;
/**
* @ORM\Column(type = "datetime")
*/
private $createdAt;
/**
* @ORM\Column(type = "datetime", nullable=true)
*/
private $dateRealisation;
/**
* @ORM\Column(type = "text")
*/
private $description;
/**
* @ORM\Column(type = "boolean")
*/
private $portfolio;
/**
* @ORM\Column(type = "string", length=255)
*/
private $slug;
/**
* @ORM\Column(type = "string", length=255)
* @var string
*/
private $file;
/**
* @var File
* @Vich\UploadableField(mapping = "Realisation", fileNameProperty = "file")
*/
private $imageFile;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy = "realisations")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\ManyToMany(targetEntity=Categorie::class, inversedBy = "realisations")
*/
private $categorie;
public function __construct()
{
$this->categorie = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getDateRealisation(): ?\DateTimeInterface
{
return $this->dateRealisation;
}
public function setDateRealisation(?\DateTimeInterface $dateRealisation): self
{
$this->dateRealisation = $dateRealisation;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getPortfolio(): ?bool
{
return $this->portfolio;
}
public function setPortfolio(bool $portfolio): self
{
$this->portfolio = $portfolio;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getFile(): ?string
{
return $this->file;
}
public function setFile(string $file): self
{
$this->file = $file;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection|Categorie[]
*/
public function getCategorie(): Collection
{
return $this->categorie;
}
public function addCategorie(Categorie $categorie): self
{
if (!$this->categorie->contains($categorie)) {
$this->categorie[] = $categorie;
}
return $this;
}
public function removeCategorie(Categorie $categorie): self
{
$this->categorie->removeElement($categorie);
return $this;
}
/**
* @return File
*/
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @param File $imageFile
*/
public function setImageFile(?File $imageFile = null)
{
$this->imageFile = $imageFile;
if (null !== $imageFile){
$this->dateRealisation = new \DateTime();
}
}
}Моя реализация
<?php
namespace App\Controller\Admin;
use App\Entity\Realisation;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Validator\Constraints\DateTime;
use Vich\UploaderBundle\Form\Type\VichFileType;
class RealisationCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Realisation::class;
}
public function configureFields(string $pageName): iterable
{
return [
IntegerField::new('id','ID')->onlyOnIndex(),
TextField::new('nom'),
TextField::new('description'),
DateTimeField::new('createdAt'),
DateTimeField::new('dateRealisation'),
TextField::new('slug'),
BooleanField::new('portfolio'),
AssociationField::new('categorie'),
TextareaField ::new('imageFile')
->setFormType(VichFileType::class)
->setLabel('Image'),
];
}
}Моя панель администратора введите описание изображения здесь
Спасибо за вашу помощь
Я хотел бы, чтобы дата создания сообщения появилась в моей базе данных, потому что тогда они отображаются на моей странице реализации и сортируются по дате, следовательно, использование createdAt
Вот ответ, мне удалось сделать то, о чем я просил. Ставлю код если может поможет
публичная функция createEntity (строка $ entityFqcn) {$ date = new \ DateTime (); $ реализация = новая реализация (); $ realisation-> setCreatedAt ($ date); return $ реализация; }




Почему бы не удалить поле или установить его только для чтения, если вы не хотите, чтобы его редактировал какой-либо пользователь?