查询时symfony 3学说一对一单向返回不需要的字段

symfony 3 doctrine one to one uni-direction returning unwanted fields when querying

我有两个 类 正在使用一对一的单向关联

{
 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

我的预期结果就是这样

{
 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 代理编码的一部分,因为您使用的是延迟加载 Doctrine 需要跟踪子实体是否需要加载。跟踪的一部分是这些功能(我相信它在教义的 this portion 中)

可能有一种方法可以避免使用延迟加载。为此,您可以使用 EAGER loading if you always want the child to load with the parent. Alternatively if you only want to use EAGER for this one query and not every time you would have to switch to DQL as documented here or you could use the JOIN comma (second example down) here