0

我在 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
                       ') ;

谢谢

4

1 回答 1

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
于 2012-02-16T03:38:25.833 回答