2016-07-19 8 views
1

私はデバイスがブランドの一部であり、デバイスがデバイスの一部であることを修復しています。今、私はブランド名またはデバイス名のいずれかでデバイスを検索することができる簡単なAJAX呼び出しを取得しようとしています。残念ながら、私は循環参照を取得します。循環参照のハンドラを作成してそれを1に制限しても、オブジェクトにはまだ多くの情報が得られます。Doctrine:循環参照を抑制する方法

は私のDQLクエリのシリアル化から次の復帰を考えてみましょう:

[{ "0":{ "ID":1、 "名": "iPhone 1"、 "ロゴ": "iPhone1 "、"ブランド ":{" id ":2、"名前 ":"アップル "、"ロゴ ":"アップル "、"デバイス ":[" iPhone 1 "、{" id ":2、" name " 「deletedAt」:null)、「deletedAt」:null、「null」、「deletedAt」:null、「deletedAt」:null、「deletedAt」: {"0":{"id":2、 "name": "iPhone"、 "logo": "iphone"、 "brand":{"}"、 "name" 「ID」:「名前」:「iPhone」「 」「ロゴ」「iPhone1」:「名前」:「Apple」「ロゴ」「apple」「devices」: 、 "deletedAt":null、 "修理":[]}、 "ブランド": "Apple"、 "deletedAt":null、 "修理":[]}、 "iPhone"名前 ":" Apple "}]]

ここでも修理は一切必要ありません。しかし、それはPartEntityで参照されているので、私はまだ多くの不必要な修復情報を取得します。オブジェクトから抜け出すデータをどのように制限できますか?

私のコントローラーコード:

public function ajaxShowDevicesAction(Request $request) { 
    //if ($request->isXmlHttpRequest()) { 
     $data = $request->query->get('data'); 
     $result = ""; 
     if ($data) { 
      $result = $this->getDoctrine()->getManager()->getRepository('AppBundle:Device')->findAllByBrandOrName($data); 
     } 
     if ($result) { 
      $encoders = array(new XmlEncoder(), new JsonEncoder()); 
      $normalizers = array(new GetSetMethodNormalizer()); 
      $normalizers[0]->setCircularReferenceHandler(function ($object) { 
       return $object->getName(); 
      }); 
      $serializer = new Serializer($normalizers, $encoders); 
      $jsoncontent = $serializer->serialize($result, 'json'); 
      $response = new Response($jsoncontent); 
      return $response; 
     } 
     // else { # unsure how to give a "no results" response 
     //  $response = new Response(json_encode(array())); 
     //  $response->headers->set('Content-Type', 'application/json'); 
     //  return $response; 
     // } 
    // } else { 
    //  throw new HttpException(403, "Ajax access only"); 
    // } 
} 

答えて

1

はあなたが正規化のための

setCircularReferenceLimit() 

メソッドを使用することができます。それが1に設定された

Handling Circular References

+0

だから私は0に設定する必要がありますか?どのデータがオブジェクトに渡されるかを判断することも可能ですか?最適化のために、デバイスのブランド名を知ることさえ常に必要なわけではないので、私はそれをカットします。 – bluppfisk