Field is not a valid object field - DoctrineEncryptBundle Symfony

I have a registration form in my application and I want to encrypt data using vmelnik-ukraine / DoctrineEncryptBundle for Symfony. Registration form - from FOSUserBundle.

I configured and installed the package and imported the @Encrypted annotation into Entity as follows:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use VMelnik\DoctrineEncryptBundle\Configuration\Encrypted;

    /**
    * Developer
    *
    * @ORM\Entity
    * @ORM\Table(name="fos_user")
    */
    class Developer extends BaseUser
    {
       ...

       /**
        * @var string
        * @ORM\Column(name="firstname", type="string", length=255)
        * @Encrypted
        * @Assert\Length(
        *      min = 2,
        *      max = 50,
        *      minMessage = "profile.register.notification.name.too-short",
        *      maxMessage = "profile.register.notification.name.too-long"
        * )
        */
       private $firstname;

But now that the form is submitted, I get the following error:

Field "firstname" is not a valid field of the entity "AppBundle\Entity\Developer" in PreUpdateEventArgs.

What am I doing wrong?

+4
source share
1 answer

I use a noticeably similar extension for Doctrine / Zend, and I ran into the same problem. I noticed that this happened every time:

  • @Encrypted (, names)
  • (, bob)
  • (, (id, bob, x, y, z))

DoctrineEncryptSubscriber.php( vmelnik-ukraine/DoctrineEncryptBundle):

public function preUpdate(PreUpdateEventArgs $args) {
    $reflectionClass = new ReflectionClass($args->getEntity());
    $properties = $reflectionClass->getProperties();
    foreach ($properties as $refProperty) {
        if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
            $propName = $refProperty->getName();
            $args->setNewValue($propName, $this->encryptor->encrypt($args->getNewValue($propName))); // this line is the problem
        }
    }
}

$args->setNewValue(...) assertValidField($field), , @Encrypted entityChangeSet... , . , . , , entityChangeSet .

, , setNewValue(), , :

public function preUpdate(PreUpdateEventArgs $args)
{
    $reflectionClass = new ReflectionClass($args->getEntity());
    $properties = $reflectionClass->getProperties();
    foreach ($properties as $refProperty) {
        if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
            $propName = $refProperty->getName();
            if ($args->hasChangedField($propName)) {
                $args->setNewValue($propName, $this->encryptor->encrypt($args->getNewValue($propName)));
            }
        }
    }
}
+2

Source: https://habr.com/ru/post/1608505/


All Articles