Symfony 3.4 : Field “montant” is not a valid field of the entity “AppBundleEntityVente” in PreUpdateEventArgs
Clash Royale CLAN TAG#URR8PPP
Symfony 3.4 : Field “montant” is not a valid field of the entity “AppBundleEntityVente” in PreUpdateEventArgs
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 "AppBundleEntityVente" in PreUpdateEventArgs.
Vente.php:
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use AppBundleValidatorConstraints as Assert2;
use DoctrineORMEventPreUpdateEventArgs;
/**
* Vente
*
* @ORMTable(name="vente")
* @ORMEntity(repositoryClass="AppBundleRepositoryVenteRepository")
* @ORMHasLifecycleCallbacks()
*/
class Vente
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DateTime
*
* @ORMColumn(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORMColumn(name="montant", type="decimal", precision=10, scale=3)
*/
private $montant;
//getters and setters
/**
* @ORMPreUpdate
*/
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 "AppBundleEntityVente" in PreUpdateEventArgs.
at vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php:130
at DoctrineORMEventPreUpdateEventArgs->assertValidField('montant')
(vendor/doctrine/orm/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php:98)
at DoctrineORMEventPreUpdateEventArgs->getNewValue('montant')
(src/AppBundle/Entity/Vente.php:118)
at AppBundleEntityVente->preUpdate(object(PreUpdateEventArgs))
(vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php:102)
at DoctrineORMEventListenersInvoker->invoke(object(ClassMetadata), 'preUpdate', object(Vente), object(PreUpdateEventArgs), 6)
(vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1060)
at DoctrineORMUnitOfWork->executeUpdates(object(ClassMetadata))
(vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:384)
at DoctrineORMUnitOfWork->commit(null)
(vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356)
at DoctrineORMEntityManager->flush()
(src/AppBundle/Controller/VenteController.php:383)
at AppBundleControllerVenteController->editAction(object(Request), object(Vente))
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:151)
at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), 1, true)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:202)
at SymfonyComponentHttpKernelKernel->handle(object(Request))
(web/app_dev.php:29)
2 Answers
2
I suppose you have a logical error in your code: you're trying to get the montant
from event
if it has not that field (look at the if
).
montant
event
if
Maybe you should modify it as follows?
if ($event->hasChangedField('montant'))
die('BB'.$event->getNewValue('montant'));
die("AA");
Your code worked in a new project.
$vente = (new Vente)
->setDate(new DateTime())
->setMontant(10)
;
$em = $this->getDoctrine()->getManager();
$em->persist($vente);
$em->flush();
$vente->setMontant(20);
$em->flush(); //dumps AA
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:
$em = $this->getDoctrine()->getManager();
$vente = $em->getRepository('AppBundle:Vente')->findOneById(1);
$vente->setDate(new DateTime());
$em->flush(); //Field "montant" is not a valid field of the entity "AppBundleEntityVente" in PreUpdateEventArgs.
You can't use $event->getNewValue('montant') without testing if montant is updated.
I cleared the cache but I still have the error. I updated the post with the exeception trace.
– Med Karim Garali
Aug 11 at 12:19
@DonCallisto answer is right You can't use $event->getNewValue('montant') if montant is not updated.
– Jamison Lima
Aug 14 at 16:44
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
your code works fine with symfony 3.4.14, I have no errors when updating data !
– Sami
Aug 10 at 19:02