Doctrine ORM 实体数据在flush时不会持久化到数据库中

问题描述

我正在更新实体 User,它与 Profiles 有 OnetoMany 双向关系,与 Contact 有 OnetoOne 关系。当我更新(刷新)用户实体时,即使返回的实体是正确的,除了 tblProfile 上的用户列之外的所有内容都会更新。

我已尝试从双方更新用户/个人资料关系,请参阅代码段的最后 2 行。两种方法都返回正确的最终实体,但表 tblProfile 未更新。

非常感谢任何帮助,我也可以提供更多部分的代码

/**
 * @ORM\Entity(repositoryClass="App\Domain\Repository\ProfileRepository")
 * @ORM\Table(name="tblProfile")
 */
class ProfileEntity
{
     /**
     * @ORM\ManyToOne(targetEntity="UserEntity",inversedBy="profiles")
     * @ORM\JoinColumn(name="PfUScId",referencedColumnName="UScId")
     */
    private ?UserEntity $user = null;

        /**
     * @return UserEntity|null
     */
    public function getUser(): ?UserEntity
    {
        return $this->user;
    }

    /**
     * @param UserEntity|null $user
     *
     * @return self
     */
    public function setUser(?UserEntity $user): self
    {
        $this->user = $user;

        return $this;
    }
}

/**
 * @ORM\Entity(repositoryClass="App\Domain\Repository\UserRepository")
 * @ORM\Table(name="tblUser")
 */
class UserEntity
{
    /**
     * @var Collection
     *
     * @ORM\OnetoMany(targetEntity="ProfileEntity",mappedBy="user")
     */
    private Collection $profiles;

     /**
     * @param array $profiles
     *
     * @return self
     */
    public function setProfiles(array $profiles): self
    {
        $this->profiles = new ArrayCollection();

        foreach ($profiles as $profile) {
            $this->profiles->add($profile);
        }

        return $this;
    }

    /**
     * @return Collection
     */
    public function getProfiles(): Collection
    {
        return $this->profiles;
    }
}

class UserService
{
    public function updateUser(
        int $legacyUserId,string $email,): UserEntity {
      $user = $this->userRepository->findOneBy(['legacyUserId' => $legacyUserId]); 
      $profiles = $user->getProfiles();


      $existingContact = $this->checkContact($user,$email);
      $Contact = $existingContact
          ? $existingContact :
          $this->contactService->createContact($email);

      $user->setContact($contact)->setId($contact->getId());

      $updatedProfiles = [];
      foreach ($profiles as $profile) {
        $updatedProfiles[] = $profile->setContact($contact)->setUser($user);
      }
      $user->setProfiles($updatedProfiles);

      $user = $this->userRepository->updateUser($user);

      foreach($updatedProfiles as $profile) {
        $this->profileService->changeUser($profile,$user);
      }


      return $user;
    }
}
 

解决方法

您想更新已分配给用户的配置文件,该配置文件具有用户 ID,因此您不必说 $user->setProfiles($updatedProfiles);,因为您将只更新已存在的配置文件。

试试这个:

  // delete this line $updatedProfiles = []; 
  foreach ($profiles as $profile) {
   // $updatedProfiles[] = to be deleted
    $profile->setContact($contact)->setUser($user);
  }
  // delete this lline $user->setProfiles($updatedProfiles); 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...