简体   繁体   中英

Doctrine Associations in symfony

so I have been working with Symfony for a while but there is one thing that bothers. 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(). Basically think that you are saving the related object to the entity and not the related field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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