duancan1732 2013-05-21 15:10
浏览 29
已采纳

获取Doctrine关联映射

I have a call that looks like the following:

$foo = $em->getRepository('MyBundle:Bar')->find($id);

I would like to be able to loop over all of the column/value pairs that get returned into $foo. I have found that in most cases the following call gets me the information I want:

public function getEntityColumnValues($entity, $em){
  $cols = $em->getClassMetadata(get_class($entity))->getColumnNames();
  $values = array();
  foreach($cols as $col){
    $getter = 'get' . $this->underscoreToCamelCase($col, true);
    $values[$col] = $entity->$getter();
  }
  return $values;
}

Sometimes, however, the entity contains some information that only exists as doctrine association mappings. That info ends out not being set in $values. Is there a way to loop over the values that get set in $foo without getting the class metadata via the getEntityColumnValues() function I have? Maybe there is a way I can enhance my function to get those mappings? Thanks.

  • 写回答

1条回答 默认 最新

  • duanjian4331 2013-05-21 16:33
    关注

    You're on the right way. $em->getClassMetadata(get_class($entity)) provides you the ClassMetadata with all the getters (eg. getAssociationMappings)you may need. Simply take a look at them and feel free to experience.

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

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合