我正在研究Symfony 3.4项目,并且在我的实体Vente.php的功能 preUpdate 中遇到@HasLifeCycleCallbacks的问题。
因此,当我更新实体字段时,不触发die('AA')并出现此错误:
Field "montant" is not a valid field of the entity "AppBundle\Entity\Vente" in PreUpdateEventArgs.
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'));
}
}
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)
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)
答案 0 :(得分:1)
我想您的代码中存在逻辑错误:如果montant
中没有event
字段,请尝试从if
中获取if ($event->hasChangedField('montant')) {
die('BB'.$event->getNewValue('montant'));
}
die("AA");
。 / p>
也许您应该对其进行如下修改?
<?php
$num = $_POST['id'];
$con = mysqli_connect("localhost", "username", "password", "movies");
$vote = "UPDATE movies SET score=score + 1 WHERE id='$num'";
$run_vote = mysqli_query($con,$vote);
?>
答案 1 :(得分:0)
您的代码在一个新项目中工作。
$vente = (new Vente)
->setDate(new \DateTime())
->setMontant(10)
;
$em = $this->getDoctrine()->getManager();
$em->persist($vente);
$em->flush();
$vente->setMontant(20);
$em->flush(); //dumps AA
也许您可以通过清除缓存来解决此问题。 您可以发送异常堆栈吗?
在@doncallisto回答之后,我测试了他的回答并发现了错误:
$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 "AppBundle\Entity\Vente" in PreUpdateEventArgs.
在未测试montant是否更新的情况下,不能使用$ event-> getNewValue('montant')。