简体   繁体   English

symfony 中的教义关联

[英]Doctrine Associations in symfony

so I have been working with Symfony for a while but there is one thing that bothers.所以我已经和 Symfony 合作了一段时间,但有一件事情很困扰。 It's about Doctrine Associations .这是关于教义协会 The thing is that I am trying to achieve a user friend invites and relations and there is a page that the user can see the invitations he sent and the ones that are pending.问题是我正在尝试实现用户好友邀请和关系,并且有一个页面可以让用户看到他发送的邀请和待处理的邀请。

EDIT: I made it happen using Many-To-One/One-To-Many associations.编辑:我使用多对一/一对多关联实现了它。 However My question is - Are Doctrine Associations the correct way of doing that.但是我的问题是 - 教义协会是这样做的正确方法吗?

My User Entity我的用户实体

class User implements UserInterface
{ 
private $id;
/**
 * @ORM\Column(name="first_name", type="string", length=30)
 *
 * @Assert\NotBlank(message="First name cannot be a blank field", groups={"register"})
 * @Assert\Length(min="3", max="30", groups={"register"})
 */

/**
 * @ORM\Column(type="string", length=50)
 *
 * @Assert\NotBlank(message="Username cannot be a blank field", groups={"register"})
 * @Assert\Length(min="7", max="50", groups={"register"})
 */
private $username;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @Assert\Length(min="7", max="50", groups={"register"})
 */
private $password;



/**
 * @ORM\OneToMany(targetEntity="App\Entity\UserInvitation", mappedBy="inviterId", orphanRemoval=true)
 */
private $userInvitations;

/**
 * @ORM\OneToMany(targetEntity="App\Entity\UserInvitation", mappedBy="invitedId", orphanRemoval=true)
 */
private $pendingUserInvitations;

//getters and setters 

My UserInvitation Entity我的用户邀请实体

class UserInvitation
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userInvitations")
 * @ORM\JoinColumn(name="inviter_id", nullable=false)
 */
private $inviterId;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pendingUserInvitations")
 * @ORM\JoinColumn(name="invited_id", nullable=false)
 */
private $invitedId;

/**
 * @ORM\Column(type="boolean")
 */
private $status;

This is my database.这是我的数据库。

我的数据库

The relationships is the right way to do it although on the entity I would use the following:关系是正确的方法,尽管在实体上我会使用以下内容:

class UserInvitation
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userInvitations")
 * @ORM\JoinColumn(name="inviter_id", nullable=false)
 */
private $inviter;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pendingUserInvitations")
 * @ORM\JoinColumn(name="invited_id", nullable=false)
 */
private $invitee;

/**
 * @ORM\Column(type="boolean")
 */
private $status;

Then you would getInviter() or setInviter().然后你会得到 getInviter() 或 setInviter()。 Basically think that you are saving the related object to the entity and not the related field基本上认为您将相关对象保存到实体而不是相关字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM