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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。