dongzhao1930 2014-11-17 10:36
浏览 73

symfony2通过getter设置值

I'd like to know if it is possible to create entities dynamically and persist them. For example I have

class Member {

private $adress; //OneToOne(targetEntity="Adress", inversedBy="Member", cascade={"persist", "remove"})

And a class Adress

class Adress
private $member //...
private $city // string

I want to set the values dynamically. I have every getter/setter name in a string (In this case Member.Adress.City, and a function that works like this (simplified)

$data = explode('.', 'Adress.City');
$entity = Object; //Here, $entity is an empty Member
$cursor = $entity;

//-1 is to target only getters, and not setters
for($i = 0; $i < (count($data) - 1); $i++) { 

    $getter = 'get' . $data[$i];
    $cursor = $cursor->$getter();
}

$setter = 'set' . $data[count($data) - 1];
$cursor->$setter($value);

$em->persist($entity);

Sadly, this doesnt work.. I can only set direct values, like Member.Name, but not relational entities. Is there a way to work this out ? Thanks !

  • 写回答

2条回答 默认 最新

  • douzhong6480 2014-11-17 10:50
    关注

    Run symfony2 cli command doctrine:generate:entities and it should create a setter and getter methods for relations as well. And then you can use them in your loop.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程