禁用Doctrine 2使用JMS Serializer时加载懒惰? [英] Disable Doctrine 2 lazy loading when using JMS Serializer?

查看:189
本文介绍了禁用Doctrine 2使用JMS Serializer时加载懒惰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Zend项目中使用了Doctrine 2 ORM,需要在几种情况下将我的实体序列化为JSON。



ATM我使用Querybuilder并加入我需要的所有表。但是我的序列化器会导致原理来懒惰地加载每个相关联的实体,这会导致相当大的数据量并引发递归。



现在我想找一个完全禁用Doctrines延迟加载行为的方法



我选择数据的方式如下:

  $ qb = $ this-> _em-> createQueryBuilder()
- > from(\Project\Entity\Personappointment,'pa')
- > select(' b','t','c','a','aps','apt','p')
- > leftjoin('pa.table','t')
- > leftjoin('pa.company','c')
- > leftjoin('pa.appointment','a')
- > leftjoin('a.appointmentstatus' aps')
- > leftjoin('a.appointmenttype','apt')
- > leftjoin('a.person','p')

我希望我的结果集只包含选择的表和关联。



任何帮助将不胜感激。

解决方案

p>在Doctrine中找到答案后,我的团队发现JMS Serializer是问题。
它自动触发了Doctrine Proxies的使用。我们为JMS Serializer编写了一个补丁,以避免Lazy Loading。



我们实现了我们自己的 DoctrineProxyHandler ,它不会触发Doctrines lazyloading机制,并在我们的SerializationHandlers Array中注册。

  class DoctrineProxyHandler implements SerializationHandlerInterface {

public function serialize(VisitorInterface $ visitor,$ data,$ type,& $ processed)
{
if(($ data instanceof Proxy || $ data instanceof ORMProxy)&&(!$ data-> __ isInitialized__ || get_class($ data)=== $ type)){
$ handles = true;

如果(!$ data-> __ isInitialized__){

//不触发doctrine延迟加载
// $ data-> __ load() ;

返回null;
}

$ navigator = $ visitor-> getNavigator();
$ navigator-> detachObject($ data);

//传递父类不加载代理类的元数据
return $ navigator-> accept($ data,get_parent_class($ data),$ visitor);
}

返回null;
}

现在我可以选择我的表,加入我需要的协会 - 而我的JSON将仅包含我选择的数据而不是无限深度关联和递归:)

  $ qb = $ this-> _em - > createQueryBuilder()
- > from(\Project\Entity\Personappointment,'pa')
- > select('pa','t','c ','a')
- > leftjoin('pa.table','t')
- > leftjoin('pa.company','c')
- > ; leftjoin('pa.appointment','a')

JSON将只包含

  {
Personappointment:{table {fields},company {fields},约会{fields}}
Personappointment:{表{fields},公司{fields},约会{fields}}
Personappointment:{table {fields},company {fields},约会{fields}}


}


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 serializer causes doctrine to lazy load every associated Entity which results in pretty huge data amounts and provokes recursion.

Now im looking for a way to totally disable Doctrines lazy loading behavior.

My way to select data would be the following:

$qb= $this->_em->createQueryBuilder()
            ->from("\Project\Entity\Personappointment", 'pa')
            ->select('pa', 't', 'c', 'a', 'aps', 'apt', 'p')
            ->leftjoin('pa.table', 't')
            ->leftjoin('pa.company', 'c')
            ->leftjoin('pa.appointment', 'a')
            ->leftjoin('a.appointmentstatus', 'aps')
            ->leftjoin('a.appointmenttype', 'apt')
            ->leftjoin('a.person','p')

I would like my resultset to only contain the selected tables and associations.

Any help would be greatly appreciated.

解决方案

After having looked for the answer in Doctrine, my team figured out that the JMS Serializer was the "problem". It triggered the use of Doctrine Proxies automatically. We wrote a Patch for JMS Serializer to avoid the Lazy Loading.

We implemented our own DoctrineProxyHandler which just doesn't trigger Doctrines lazyloading mechanism and registered it within our SerializationHandlers Array.

class DoctrineProxyHandler implements SerializationHandlerInterface {

public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
{
    if (($data instanceof Proxy || $data instanceof ORMProxy) && (!$data->__isInitialized__ || get_class($data) === $type)) {
        $handled = true;

        if (!$data->__isInitialized__) {

            //don't trigger doctrine lazy loading
            //$data->__load();

            return null;
        }

        $navigator = $visitor->getNavigator();
        $navigator->detachObject($data);

        // pass the parent class not to load the metadata for the proxy class
        return $navigator->accept($data, get_parent_class($data), $visitor);
    }

    return null;
}

Now i can simply select my table, join the associations i need - and my JSON will contain just the data i selected instead of infinite depth associations and recursions :)

$qb= $this->_em->createQueryBuilder()
        ->from("\Project\Entity\Personappointment", 'pa')
        ->select('pa', 't', 'c', 'a')
        ->leftjoin('pa.table', 't')
        ->leftjoin('pa.company', 'c')
        ->leftjoin('pa.appointment', 'a')

JSON will just contain

{  
  Personappointment: { table {fields}, company {fields}, appointment {fields}}
  Personappointment: { table {fields}, company {fields}, appointment {fields}}
  Personappointment: { table {fields}, company {fields}, appointment {fields}}
  .
  .
}

这篇关于禁用Doctrine 2使用JMS Serializer时加载懒惰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆