1

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

{
 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)
4

1 回答 1

2

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

可能有一种方法可以避免使用延迟加载。为此,如果您总是希望孩子与父母一起加载,您可以使用EAGER 加载。或者,如果您只想对这个查询使用 EAGER,而不是每次都必须切换到此处记录的 DQL,或者您可以在此处使用 JOIN 逗号(第二个示例)

于 2017-04-04T23:07:52.867 回答