FOSUserBundle 与我自己的表建立关系

发布于 2025-01-06 01:25:07 字数 816 浏览 0 评论 0原文

我在 symfony2 中使用 FOSUserBundle,它工作得很好,但我必须创建另一个表,例如 Entry,其中包含字段:

 user_id, description,...

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

我收到此错误:

条目没有名为 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
                       ') ;

谢谢

Im using the FOSUserBundle in symfony2, it's works very well but I have to create for example another table like Entry, with fields:

 user_id, description,...

I need to make a relationship between the user table and Entry table. What is the best way to make the relationship One to Many? Which class I should extend?

I get this error:

Entry has no association named ApplicationSonataUserBundle:User

My code is:

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

protected $users;

I use this instruccion:

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-01-13 01:25:07

用户实体:

/**
* @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

Twig:

{% 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

User Entity:

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

New Entity:

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

From the controller

$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

Twig:

{% 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文