Symfony 将表单与数据库进行比较

Symfony compare form to database

如何将提交的表单字段与实际数据库数据进行比较?

如果我使用我的存储库获取实体的副本,它实际上只会获取提交的表单的副本。我想知道表单提交的数据是否和表单有变化

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() 是答案,可在此处找到 Source

这将查询数据库并且不使用注入验证器的缓存版本。

$original = $this->em->getUnitOfWork()->getOriginalEntityData($object);