Symfony4 Doctrine 不更新数据库
Symfony4 Doctrine does not update database
我正在尝试使用 ManyToMany 自引用的原则和控制器更新数据库,问题是我没有错误,但是 MySQL 在函数之后没有更新所以我不知道是什么我搞砸了。就像没有附加的一样。
有没有办法更新 MySql 或至少进行调试?
这是我的代码:
/**
* @ManyToMany(targetEntity="User")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_a_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="user_b_id", referencedColumnName="id")}
* )
* @var ArrayCollection
*/
private $friends;
/**
* Constructor.
*/
public function __construct()
{
$this->friends = new ArrayCollection();
}
/**
* @return array
*/
public function getFriends()
{
return $this->friends->toArray();
}
/**
* @param User $user
* @return void
*/
public function addFriend(User $user)
{
if (!$this->friends->contains($user)) {
$this->friends->add($user);
$user->addFriend($this);
}
}
/**
* @param User $user
* @return void
*/
public function removeFriend(User $user)
{
if ($this->friends->contains($user)) {
$this->friends->removeElement($user);
$user->removeFriend($this);
}
}
和我的控制器:
/**
* @Route("/addFriends")
*/
public function addFriends()
{
if (isset($_POST['add'])) {
$id = $_POST['id'];
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)-
>find($id);
$user->addFriend($friend);
}
return $this->redirectToRoute('friends');
}
/**
* @Route("/removeFriends")
*/
public function removeFriends()
{
if (isset($_POST['add'])) {
$id = $_POST['id'];
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)->find($id);
$user->removeFriend($friend);
}
return $this->redirectToRoute('friends');
}
好的,所以我终于看到我搞砸了什么,这很简单,当我将用户添加到我的 arrayCollection 时,我的更改没有保存,所以我最终得到一个空数组,要保存更改,只需添加:
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
最后我的控制器看起来像这样:
public function addFriends(Request $request)
{
if (isset($request) && is_string($id = $request->get('id'))) {
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)->find($id);
$user->addFriend($friend);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
return $this->redirectToRoute('friends');
}
希望这对某人有所帮助!
我正在尝试使用 ManyToMany 自引用的原则和控制器更新数据库,问题是我没有错误,但是 MySQL 在函数之后没有更新所以我不知道是什么我搞砸了。就像没有附加的一样。 有没有办法更新 MySql 或至少进行调试?
这是我的代码:
/**
* @ManyToMany(targetEntity="User")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_a_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="user_b_id", referencedColumnName="id")}
* )
* @var ArrayCollection
*/
private $friends;
/**
* Constructor.
*/
public function __construct()
{
$this->friends = new ArrayCollection();
}
/**
* @return array
*/
public function getFriends()
{
return $this->friends->toArray();
}
/**
* @param User $user
* @return void
*/
public function addFriend(User $user)
{
if (!$this->friends->contains($user)) {
$this->friends->add($user);
$user->addFriend($this);
}
}
/**
* @param User $user
* @return void
*/
public function removeFriend(User $user)
{
if ($this->friends->contains($user)) {
$this->friends->removeElement($user);
$user->removeFriend($this);
}
}
和我的控制器:
/**
* @Route("/addFriends")
*/
public function addFriends()
{
if (isset($_POST['add'])) {
$id = $_POST['id'];
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)-
>find($id);
$user->addFriend($friend);
}
return $this->redirectToRoute('friends');
}
/**
* @Route("/removeFriends")
*/
public function removeFriends()
{
if (isset($_POST['add'])) {
$id = $_POST['id'];
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)->find($id);
$user->removeFriend($friend);
}
return $this->redirectToRoute('friends');
}
好的,所以我终于看到我搞砸了什么,这很简单,当我将用户添加到我的 arrayCollection 时,我的更改没有保存,所以我最终得到一个空数组,要保存更改,只需添加:
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
最后我的控制器看起来像这样:
public function addFriends(Request $request)
{
if (isset($request) && is_string($id = $request->get('id'))) {
$user = $this->getUser();
$friend = $this->getDoctrine()->getRepository(User::class)->find($id);
$user->addFriend($friend);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
return $this->redirectToRoute('friends');
}
希望这对某人有所帮助!