Symfony2 PRE_SET_DATA $event->getData() return wro

2020-04-13 04:15发布

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条回答
Anthone
2楼-- · 2020-04-13 05:06

I need to use this condition for the getter work :

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

        }
查看更多
登录 后发表回答