Reputation: 13
I'm working on a project with symfony 6 and easyadmin 4 to create an administration service for a site. I have dozens of entities and especially one entity (Entity_History) that is used to record actions performed on other entities by a service. Given the number of entities, there is no relationship between these entities and the Entity_History entity. But in the index page of each entity, I'd like to retrieve the last modification date of each record. I've made a HistoryTrait to do this and it's currently blocking. In fac, the query executes, but when I have a value, I get the “unreachable” result on the record's line, and "none" when there's no value to retrieve. (I'm not an English speaker, so I don't know the exact English terms.) I don't know what could be blocking the rendering of the transaction
I try to make A trait (and a service before) to retrieve data.
My CRUDController (one of the ten entities that must retrieve the date from Entity_History)
<?php
namespace App\Controller\Admin;
use App\Entity\LocationType;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
class LocationTypeCrudController extends AbstractCrudController
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getEntityFqcn(): string
{
return LocationType::class;
}
public function configureActions(Actions $actions): Actions
{
return $actions
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setLabel('Ajouter un typer de lieu');
})
->update(Crud::PAGE_NEW, Action::SAVE_AND_RETURN, function (Action $action) {
return $action->setLabel('Créer le type de lieu');
})
->update(Crud::PAGE_NEW, Action::SAVE_AND_ADD_ANOTHER, function (Action $action) {
return $action->setLabel('Créer et ajouter un autre type de lieu');
})
->update(Crud::PAGE_INDEX, Action::EDIT, function (Action $action) {
return $action
->setIcon('fa fa-edit')
->setLabel(false)
->setHtmlAttributes([
'title' => 'Modifier cet élément',
])
->displayAsLink()
->addCssClass('btn btn-sm btn-light');
})
->update(Crud::PAGE_INDEX, Action::DELETE, function (Action $action) {
return $action
->setIcon('fa fa-trash')
->setLabel(false)
->setHtmlAttributes([
'title' => 'Supprimer cet élément',
])
->displayAsLink()
->addCssClass('btn btn-sm btn-light');
});
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->addFormTheme('admin/form.html.twig')
->setEntityLabelInSingular('Type de lieu')
->setEntityLabelInPlural('Type de lieux')
->setPageTitle('new', 'Ajouter un nouveau type de lieu')
->showEntityActionsInlined();
}
public function configureFields(string $pageName): iterable
{
return [
TextField::new('type'),
DateTimeField::new('lastModificationDate')
->setLabel('Dernière modification')
->setVirtual(true)
->formatValue(function ($value, $entity) {
return $entity->getLastModificationDate($this->entityManager);
})
->onlyOnIndex(),
];
}
}
HistoryTrait to make relation beetween one fo entity and Entity_History
<?php
namespace App\Controller\Admin\Trait;
use App\Entity\EntityHistory;
use Doctrine\ORM\EntityManagerInterface;
trait HistoryTrait
{
public function getLastModification(EntityManagerInterface $entityManager): ?EntityHistory
{
return $entityManager->getRepository(EntityHistory::class)
->findOneBy(
['entityName' => get_class($this), 'entityId' => $this->getId()],
['dateAction' => 'DESC']
);
}
public function getLastModificationDate(EntityManagerInterface $entityManager): ?\DateTimeInterface
{
$lastModification = $this->getLastModification($entityManager);
return $lastModification ? $lastModification->getDateAction() : null;
}
public function getLastModificationUser(EntityManagerInterface $entityManager): ?string
{
$lastModification = $this->getLastModification($entityManager);
return $lastModification ? $lastModification->getUser() : null;
}
public function getLastModificationAction(EntityManagerInterface $entityManager): ?string
{
$lastModification = $this->getLastModification($entityManager);
return $lastModification ? $lastModification->getAction() : null;
}
}
Entity_History
<?php
namespace App\Entity;
use App\Repository\EntityHistoryRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EntityHistoryRepository::class)]
class EntityHistory
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $entityName = null;
#[ORM\Column]
private ?int $entityId = null;
#[ORM\Column(length: 255)]
private ?string $action = null;
#[ORM\Column(type: 'json')]
private array $oldValues = [];
#[ORM\Column(type: 'json')]
private $newValues = [];
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $dateAction = null;
#[ORM\ManyToOne(inversedBy: 'entityHistories')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): static
{
$this->id = $id;
return $this;
}
public function getEntityName(): ?string
{
return $this->entityName;
}
public function setEntityName(string $entityName): static
{
$this->entityName = $entityName;
return $this;
}
public function getEntityId(): ?int
{
return $this->entityId;
}
public function setEntityId(int $entityId): static
{
$this->entityId = $entityId;
return $this;
}
public function getAction(): ?string
{
return $this->action;
}
public function setAction(string $action): static
{
$this->action = $action;
return $this;
}
public function getOldValues(): array
{
return $this->oldValues;
}
public function setOldValues(array $oldValues): static
{
$this->oldValues = $oldValues;
return $this;
}
public function getOldValuesJson(): string
{
$json = json_encode($this->oldValues);
return $json;
}
public function getNewValuesJson(): string
{
$json = json_encode($this->newValues);
return $json;
}
public function getNewValues(): array
{
return $this->newValues;
}
public function setNewValues(array $newValues): static
{
$this->newValues = $newValues;
return $this;
}
public function getDateAction(): ?\DateTimeInterface
{
return $this->dateAction;
}
public function setDateAction(\DateTimeInterface $dateAction): static
{
$this->dateAction = $dateAction;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
}
Upvotes: 0
Views: 98