我有两个使用一对一单向关联的课程
{
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)
答案 0 :(得分:1)
这些函数是Doctrine代理编码的一部分,因为您正在使用Lazy Loading Doctrine需要跟踪子实体是否需要加载。保持跟踪的部分原因是这些功能(我相信它在Doctrine的this portion中)
可能有一种方法可以避免使用延迟加载。要做到这一点,如果您总是希望孩子加载父母,可以使用EAGER loading。或者,如果您只想为此一个查询使用EAGER,而不是每次都必须将{DQL改为documented here,或者您可以使用JOIN逗号(第二个示例向下)here