2011-05-15 21:29
浏览 34

你如何在Doctrine 2中使用实体关系?

When you want to insert an Entity you do this:

$user = new User();


But what if I want to create an article which can have one User;

Currently, I need to do:

$user = $em->getRepository('User')->find($id);

This is because of the relationship, Doctrine 2 asks for an User entity.

However, I can't "mock" an User object, because I don't want the id be set manually, therefore I can't do:

$user = new User();

Am I wrong about this behavior, how do you do?

It can be performance matter to load the User entity just to set the relationship, even with a cache, which cannot be always an option, especially for an update.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmeng1402 2011-05-15 23:03

    If you don't have a managed User entity handy, what you want is a reference proxy, which the EM will be happy to give you:

    $article = new Entity\Article();
    $article->setTitle('Reference Proxies Rule');
    解决 无用
    打赏 举报

相关推荐 更多相似问题