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

给你一囗甜甜゛ 提交于 2019-12-01 23:10:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!