I'm working into a Symfony 3.4 Project and I get an issue with @HasLifeCycleCallbacks in function preUpdate for my entity Vente.php.
So when I update my entity fields, the die('AA') is not fired and I get this error :
Field "montant" is not a valid field of the entity "AppBundle\Entity\Vente" in PreUpdateEventArgs.
Vente.php:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Validator\Constraints as Assert2;
use Doctrine\ORM\Event\PreUpdateEventArgs;
/**
* Vente
*
* @ORM\Table(name="vente")
* @ORM\Entity(repositoryClass="AppBundle\Repository\VenteRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Vente
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="montant", type="decimal", precision=10, scale=3)
*/
private $montant;
//getters and setters
/**
* @ORM\PreUpdate
*/
public function preUpdate(PreUpdateEventArgs $event)
{
if ($event->hasChangedField('montant')) {
die("AA");
}
die('BB'.$event->getNewValue('montant'));
}
}
Exception:
in vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php (line 130)
PreUpdateEventArgs->assertValidField('montant') in vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php (line 98)
PreUpdateEventArgs->getNewValue('montant') in src/AppBundle/Entity/Vente.php (line 118)
public function preUpdate(PreUpdateEventArgs $event)
{
if ($event->hasChangedField('montant')) {
die("AA");
}
die('BB'.$event->getNewValue('montant'));
Vente->preUpdate(object(PreUpdateEventArgs)) in vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php (line 102)
ListenersInvoker->invoke(object(ClassMetadata), 'preUpdate', object(Vente), object(PreUpdateEventArgs), 6) in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php (line 1060)
UnitOfWork->executeUpdates(object(ClassMetadata)) in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php (line 384)
UnitOfWork->commit(null) in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php (line 356)
EntityManager->flush() in src/AppBundle/Controller/VenteController.php (line 383)
VenteController->editAction(object(Request), object(Vente)) in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php (line 151)
HttpKernel->handleRaw(object(Request), 1) in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php (line 68)
HttpKernel->handle(object(Request), 1, true) in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php (line 202)
Kernel->handle(object(Request)) in web/app_dev.php (line 29)
Stack Trace:
InvalidArgumentException:
Field "montant" is not a valid field of the entity "AppBundle\Entity\Vente" in PreUpdateEventArgs.
at vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php:130
at Doctrine\ORM\Event\PreUpdateEventArgs->assertValidField('montant')
(vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php:98)
at Doctrine\ORM\Event\PreUpdateEventArgs->getNewValue('montant')
(src/AppBundle/Entity/Vente.php:118)
at AppBundle\Entity\Vente->preUpdate(object(PreUpdateEventArgs))
(vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php:102)
at Doctrine\ORM\Event\ListenersInvoker->invoke(object(ClassMetadata), 'preUpdate', object(Vente), object(PreUpdateEventArgs), 6)
(vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1060)
at Doctrine\ORM\UnitOfWork->executeUpdates(object(ClassMetadata))
(vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:384)
at Doctrine\ORM\UnitOfWork->commit(null)
(vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356)
at Doctrine\ORM\EntityManager->flush()
(src/AppBundle/Controller/VenteController.php:383)
at AppBundle\Controller\VenteController->editAction(object(Request), object(Vente))
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:151)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:202)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(web/app_dev.php:29)
I suppose you have a logical error in your code: you're trying to get the
montant
fromevent
if it has not that field (look at theif
).Maybe you should modify it as follows?
Your code worked in a new project.
Maybe you can solve this by clearing your cache. Can you send the exception stack?
After @doncallisto answer, I tested his answer and found the error:
You can't use $event->getNewValue('montant') without testing if montant is updated.