问题
When I try to get the data from event PRE_SET_DATA, I get my object with good value, but I can't use it.
This is my test code :
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($factory){
$data = $event->getData();
print_r($data);
}
);
This returns a long text :
"YOU\CommercantBundle\Entity\LivraisonChoix Object ( [id:YOU\CommercantBundle\Entity\LivraisonChoix:private] => 22 ..."
But when I use a getter :
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($factory){
$data = $event->getData();
print_r($data->getId());
}
);
I get an error :
FatalErrorException: Error: Call to a member function getId() on a non-object
How can I access to data?
This work fine for PRE_BIND event.
回答1:
I need to use this condition for the getter work :
if ($data instanceof \YOU\CommercantBundle\Entity\LivraisonChoix) {
}
来源:https://stackoverflow.com/questions/17319277/symfony2-pre-set-data-event-getdata-return-wrong-object