FOSUserBundle与我自己的表建立关系

时间:2012-02-16 03:17:45

标签: symfony fosuserbundle symfony-sonata

我在symfony2中使用FOSUserBundle,它运行得很好,但我必须创建另一个像Entry这样的表,其中包含字段:

 user_id, description,...

我需要在user表和Entry表之间建立关系。建立一对多关系的最佳方式是什么?我应该扩展哪一堂课?

我收到此错误:

Entry has no association named ApplicationSonataUserBundle:User

我的代码是:

   /**
 *
 * @ORM\ManyToOne(targetEntity="\Application\Sonata\UserBundle\Entity\User",  inversedBy="entry")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/

protected $users;

我使用这个指令:

   $em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery('SELECT b
                               FROM LoggerFrontendBundle:Entry a JOIN a.ApplicationSonataUserBundle:User b
                       ') ;

由于

1 个答案:

答案 0 :(得分:0)

用户实体:

/**
* @ORM\OneToOne(targetEntity="path to new Entity", mappedBy="user", cascade={"persist"})
*/
protected $name;

新实体:

/**
* @ORM\OneToOne(targetEntity="path to User Entity", inversedBy="name")
*/
protected $user;

来自控制器

$em = $this->getDoctrine()->getEntityManager();
// when you query users, you get the information from the new Entity.
$users = $em->getRepository('ApplicationSonataUserBundle:User')->findAll();

return array('users' => $user); // return the array of user information

嫩枝:

{% for user in users %} // loop through users<br/>

{{ user.name.getName }} // you can call user.getname or user.getEmail , you can add the name from the Entity on and get the fields from the new Entity like user.name.getName etc...

{% endfor %} //end loop