Disable Doctrine 2 lazy loading when using JMS Serializer?

前端 未结 5 1332
梦谈多话
梦谈多话 2020-12-05 11:24

Im using Doctrine 2 ORM in my Zend project and need to serialize my Entities to JSON in several cases.

ATM i use the Querybuilder and join all tables i need. But my

5条回答
  •  醉话见心
    2020-12-05 12:11

    In the latest version of JMSSerializer, the place you should look at is

    JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber
    

    instead of

    Serializer\Handler\DoctrineProxyHandler
    

    To override the default lazy load behavior, one should define his own event subscriber.

    In your app/config.yml add this:

    parameters:
        ...
        jms_serializer.doctrine_proxy_subscriber.class: Your\Bundle\Event\DoctrineProxySubscriber
    

    you can copy the class from JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber to Your\Bundle\Event\DoctrineProxySubscriber and comment out the $object->__load(); line

    public function onPreSerialize(PreSerializeEvent $event)
    {
        $object = $event->getObject();
        $type = $event->getType();
    
        // If the set type name is not an actual class, but a faked type for which a custom handler exists, we do not
        // modify it with this subscriber. Also, we forgo autoloading here as an instance of this type is already created,
        // so it must be loaded if its a real class.
        $virtualType = ! class_exists($type['name'], false);
    
        if ($object instanceof PersistentCollection) {
            if ( ! $virtualType) {
                $event->setType('ArrayCollection');
            }
    
            return;
        }
    
        if ( ! $object instanceof Proxy && ! $object instanceof ORMProxy) {
            return;
        }
    
         //$object->__load(); Just comment this out
    
        if ( ! $virtualType) {
            $event->setType(get_parent_class($object));
        }
    }
    

提交回复
热议问题