Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 PRE_SET_DATA $event->getData() return wrong object

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.

like image 501
Paul T. Avatar asked May 14 '26 15:05

Paul T.


1 Answers

I need to use this condition for the getter work :

        if ($data instanceof \YOU\CommercantBundle\Entity\LivraisonChoix) {

        }
like image 89
Paul T. Avatar answered May 17 '26 05:05

Paul T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!