doujiao1814 2017-04-04 22:19
浏览 60
已采纳

symfony 3 doctrine在查询时一对一地返回不需要的字段

I have got two classes which are being associated using one to one uni-direction

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

the above is the result when I do query like following

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

I want to know where and why the following come from

__initializer__: null,
__cloner__: null,
__isInitialized__: true

my expected result is just this

{
 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 ...>

This is the method in controller

/**
 * @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;   
}

This is what is inside in 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条回答 默认 最新

  • douli8428 2017-04-04 23:07
    关注

    Those functions are part of the Doctrine proxy coding, since you are using Lazy Loading Doctrine needs to keep track of the child entity if it needs to be loaded or not. Part of that keeping track is these functions (I believe it is in this portion of Doctrine)

    There may be a way around this which would be to avoid using lazy loading. To do that you can utilize 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题