symfony的3教义一对一的单向查询

问题描述:

正被使用一对一的单向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) 

这些函数是Doctrine代理编码的一部分,因为您正在使用Lazy Loading Doctrine需要跟踪子实体是否需要加载。部分原因是这些功能(我相信它在Doctrine的this portion中)

可能有一种方法可以避免使用延迟加载。要做到这一点,如果您始终希望孩子与父母一起加载,则可以使用EAGER loading。或者,如果您只想使用EAGER进行这一个查询,而不是每次您必须切换到DQL为documented here,或者您可以使用JOIN逗号(第二个示例向下)here