0

I have two entities : Document and DocumentValidation

Document :

class Document
{
    ...

    /**
     * @ORM\OneToMany(
     *     targetEntity="AppBundle\Entity\DocumentValidation",
     *     mappedBy="document",
     *     cascade={"persist"}
     * )
     */
    private $validations;

DocumentValidation :

class DocumentValidation
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", nullable=true)
     * @Assert\DateTime
     */
    private $validatedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", nullable=true)
     * @Assert\DateTime
     */
    private $deletedAt;

Each Document can validated and deleted many times. But a document can only have one validation in progress : Validation, deletion, validation, deletion, validation, deletion, validation, ...

In Twig, I list all Documents. And I want to check if each document can be deletable. If a document has a validation in progress, it can not be deleted and hide delete icon.

What is the best method ?

  • If I use Doctrine query in Symfony Voter with is_granted() for check if each document has validation in progress, I have many many many queries ... (one per Document) ;
  • If I use LEFT JOIN in Controller (and Symfony Voter), it's very difficult if I have many controls (read [PLEASE] at below ) ;
  • Create a Class with Filesystemcache and save if each Document can be deleted. Regenerate the cache at each linked entity manipulation ;
  • Other ?

PLEASE, keep in mind that this situation is very simple. In my situation, I have many reasons (> 10) to reject a deletion or modification, almost always because of a relationship in database

Gaylord.P
  • 1,539
  • 2
  • 24
  • 54
  • why complicating things? can't you just check if `validatedAt > deletedAt` then show delete icon (because the entity has been validated and now can be deleted), else hide? – genesst Oct 08 '17 at 20:30

1 Answers1

0

If you want to avoid many queries or many controls may be the best solution will be to add a new field canBeDeletable in your Document entity and handle the value of this field for each action that have an impact on it.

You can find a way to do it properly (with custom events by example).

At the end, there will be only one voter to create without many queries or controls, just one that will check the value of this field.

Fabien Salles
  • 1,101
  • 15
  • 24