简体   繁体   English

Symfony2 PRE_SET_DATA $ event-> getData()返回错误的对象

[英]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. 当我尝试从事件PRE_SET_DATA获取数据时,我得到了具有良好价值的对象,但我无法使用它。

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 ..." “你\\ CommercantBundle \\ Entity \\ LivraisonChoix对象([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 FatalErrorException:错误:在非对象上调用成员函数getId()

How can I access to data? 我如何访问数据?

This work fine for PRE_BIND event. 这适用于PRE_BIND事件。

I need to use this condition for the getter work : 我需要将此条件用于getter工作:

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM