Symfony 3: How to sync users using Doctrine

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Symfony 3: How to sync users using Doctrine



I wanted to know how to sync users for example on my user table (fos_user) I want to sync username with another tables.



So if I want to see who is the user that posted a comment I want to do



$commentA->getUser();



and it will show the user of the user table (fos_user), for that when the user change his username the comment will get his new username.



Thanks for help




2 Answers
2



What you are looking for are database relationships - OneToMany / ManyToOne in doctrine - See in documentation - https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirectional



Then you use your primary key as connecting key, not username.



I think you need something like this,



In your User class you need this property and methods:


User


property


methods


/**
* Class User
* @ORMTable(name="`user`")
*/
class User extends FosUser

[Your other properties]

/**
* @var Collection
*
* @ORMOneToMany(targetEntity="Comment", mappedBy="user")
*/
protected $comments;

/**
* User constructor.
*/
public function __construct()

parent::__construct();
$this->comments = new ArrayCollection();


/**
* @return Collection
*/
public function getComments(): Collection

return $this->comments;


/**
* @param Comment $comment
*/
public function addComment(Comment $comment): void

if ($this->getComments()->contains($comment))

return;
else

$this->getComments()->add($comment);
$comment->setUser($this);



/**
* @param Comment $comment
*/
public function removeComment(Comment $comment): void

if (!$this->getComments()->contains($comment))

return;
else

$this->getComments()->removeComment($comment);
$comment->setUser(null);





And in your Comment class you need this property and methods:


Comment


property


methods


/**
* Class User
* @ORMTable(name="`comment`")
*/
class Comment

[Your other properties]
/**
* @var User
*
* @ORMManyToOne(targetEntity="User", inversedBy="comments")
*/
protected $user;

/**
* @return User
*/
public function getUser(): User

return $this->user;


/**
* @param User $user
*/
public function setUser(User $user): void

/** Maybe you need this too, if get exception delete this line */
$user->addComment($this);
$this->user = $user;




Maybe you need cascade="persist" in your annotation in User class or Comment class ... depend on your logic.


cascade="persist"



something like this line


* @ORMOneToMany(targetEntity="Comment", mappedBy="user", cascade"persist")



now your comments connected to owner user, and if user change, commit user change too, and you must use class user like this way:


$comment->getUser()->getUsername();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard