2017-04-04 33 views
0

正被使用一對一的單向symfony的3教義一對一的單向查詢

{ 
id: 1, 
name: "onetooneuniparent name", 
onetooneunichild: { 
    id: 1, 
    name: "onetooneunichild name", 
    __initializer__: null, 
    __cloner__: null, 
    __isInitialized__: true 
    } 
} 

以上是結果,當我查詢等相關聯的我有兩個類時返回不需要的字段以下

http://localhost:8000/onetooneRead?id=1 

我想知道,爲什麼下面來自

__initializer__: null, 
__cloner__: null, 
__isInitialized__: true 

我EXPE反恐執行局的結果就是這個

{ 
id: 1, 
name: "onetooneuniparent name", 
onetooneunichild: { 
    id: 1, 
    name: "onetooneunichild name" 
    } 
} 

OnetoOneUniParent.php

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="onetooneuniparent") 
*/ 

class OnetoOneUniParent{ 

/** 
* @ORM\Column(type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\Column(type="string",name="name")  
*/ 
private $name; 

/** 
* @ORM\OneToOne(targetEntity="OnetoOneUniChild",cascade={"persist"}) 
* @ORM\JoinColumn(name="child_id", referencedColumnName="id") 
*/ 
private $onetooneunichild; 

<.... getter and setter here ...> 
} 

OnetoOneUniChild.php

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="onetooneunichild") 
*/ 
class OnetoOneUniChild{ 

/** 
* @ORM\Column(type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\Column(type="string",name="name")  
*/ 
private $name; 

<.... getter and setter here ...> 

這是控制器的方法

/** 
* @Route("/onetooneRead") 
* @Method("GET") 
*/ 
public function onetooneReadAction(Request $request){ 
    $logger = $this->get('logger'); 
    $encoders = array(new XmlEncoder(), new JsonEncoder()); 
    $normalizers = array(new ObjectNormalizer()); 

    $serializer = new Serializer($normalizers, $encoders); 

    $logger->info('onetoone Read'); 

    $id = $request->query->get("id"); 

    $em = $this->getDoctrine()->getManager(); 
    $onetooneuniparent = $em->getRepository('AppBundle:OnetoOneUniParent')->find($id); 

    $onetooneuniparentJson = $serializer->serialize($onetooneuniparent, 'json'); 

    $response = new JsonResponse(); 

    $response->setContent($onetooneuniparentJson); 

    return $response; 
} 

這裏面是什麼在MySQL

mysql> select * from onetooneuniparent; 
+----+----------+------------------------+ 
| id | child_id | name     | 
+----+----------+------------------------+ 
| 1 |  1 | onetooneuniparent name | 
| 2 |  2 | onetooneuniparent name | 
| 3 |  3 | onetooneuniparent name | 
+----+----------+------------------------+ 
3 rows in set (0.00 sec) 

mysql> select * from onetooneunichild; 
+----+-----------------------+ 
| id | name     | 
+----+-----------------------+ 
| 1 | onetooneunichild name | 
| 2 | onetooneunichild name | 
| 3 | onetooneunichild name | 
+----+-----------------------+ 
3 rows in set (0.00 sec) 

回答

1

這些函數是Doctrine代理編碼的一部分,因爲您正在使用Lazy Loading Doctrine需要跟蹤子實體是否需要加載。部分原因是這些功能(我相信它在Doctrine的this portion中)

可能有一種方法可以避免使用延遲加載。要做到這一點,如果您始終希望孩子與父母一起加載,則可以使用EAGER loading。或者,如果您只想使用EAGER進行這一個查詢,而不是每次您必須切換到DQL爲documented here,或者您可以使用JOIN逗號(第二個示例向下)here