How can I compare a form field submitted to the actual database data?
If I use my repository to get a copy of the entity, it actually just gets a copy of the form submitted. I want to know if the data submitted in the form has changed from the form.
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\ORM\EntityManagerInterface;
use \App\Entity\Logistics\CarrierSendle;
use Symfony\Component\Security\Core\Security;
class CarrierDescUniqueValidator extends ConstraintValidator {
/**
* @var Security
*/
private $security;
/**
* @var Em
*/
private $em;
public function __construct(EntityManagerInterface $em, Security $security) {
$this->em = $em;
$this->security = $security;
}
public function validate($object, Constraint $constraint) {
/* @var $constraint \App\Validator\CarrierDescUnique */
if (!$constraint instanceof CarrierDescUnique) {
throw new UnexpectedTypeException($constraint, CarrierDescUnique::class);
}
if (null === $object->getDescription() || '' === $object->getDescription()) {
return;
}
$repoCurrent = $this->em->getRepository(CarrierSendle::class)
->GetCurrentName($this->security->getUser(), $object->getSendleId());
if ($object->getDescription() === $repoCurrent->getDescription()) {
return;
}
var_dump($object); var_dump($repoCurrent); die;
...
}
}
UnitOfWork() is the answer, found here Source
This will query the database and not used the cached version injected into the validator.
$original = $this->em->getUnitOfWork()->getOriginalEntityData($object);