2012-02-16 72 views
0

即時通信關係,它的作品非常好,但我要創建例如像進入另一個表,與字段:FOSUserBundle使在Symfony2中使用FOSUserBundle我自己的表

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; 

我用這個instruccion:

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

感謝

回答

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 
+0

還算可以,但我必須刷新我的問題,因爲我嘗試但是當我不工作 – JERC 2012-02-16 15:15:04