0

I'm currently working on a synfomy 4.2 project using sonata admin and Doctrine ORM.

I would like to abort a query execution when some condition is set to true, for example, if I would like to insert a new Product, but the Product for some reason laks of the parameter price then I would like to abort that query and set a flash message to make the user know that the Product could not be created because it laks of price.

I have done my research but I cant find anything that relates to my problem, I do know that this functionality must be applied in a PrePersist function on the Entity.

I have been breaking my head with something like this.

/**
* @ORM\PrePersist
*/
public function prePersist(LifecycleEventArgs $args)
{
    $em = $args->getEntityManager("App\Milenio\VersionsControlBundle\Entity\Plugins");
    $em->getConnection();
    $em->flush();
    $em->clear();
}

But of course this does nothing, I'm still causing an obvious SQL exception.

I know there are ways to tell sonata admin to make an input required but in my particular case that wont make the trick.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Benjamin Gil
  • 127
  • 1
  • 12

1 Answers1

0

Would it not make more sense to force the user to fill in the price, or to just make the price not nullable, and catch the error in the controller, and then message the user, these things are cleaner solutions and don't require listeners. Furthermore I don't think you can flush in a prepersist, maybe entitymanager->rollback, or entitymanager detach are what you are looking for

T. Kelher
  • 1,188
  • 10
  • 9