Reputation: 1342
I am getting the error "A new entity was found through the relationship 'App\Entity\UserProfileMatch#profile'" with the code below in my Symfony / Doctrine app.
Details:
I have the following Profile entity:
<?php
#[ORM\Entity(repositoryClass: ProfileRepository::class)]
#[ORM\Table(name: '`profile`')]
class Profile
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: 'integer', unique: true)]
private ?int $userId = null;
#[ORM\Column(type: 'integer', unique: true)]
private ?int $profileTypeId = null;
// Relationships
#[ORM\ManyToOne(targetEntity: ProfileType::class)]
#[ORM\JoinColumn(name: 'profile_type_id', referencedColumnName: 'id', nullable: false)]
private ?ProfileType $profileType = null;
#[ORM\OneToMany(targetEntity: UserProfileMatch::class, mappedBy: 'profile', cascade: ['persist'])]
private Collection $userProfileMatch;
// .. getters and setters
}
?>
And the following UserMatchProfile entity:
<?php
#[ORM\Entity(repositoryClass: UserProfileMatchRepository::class)]
#[ORM\Table(name: '`user_profile_match`')]
class UserProfileMatch
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
private ?int $profileId = null;
#[ORM\Id]
#[ORM\Column(type: 'integer')]
private ?int $profileTypeId = null;
#[ORM\ManyToOne(targetEntity: Profile::class, cascade: ['persist'], inversedBy: 'UserProfileMatch')]
#[ORM\JoinColumn(nullable: false)]
private ?Profile $profile = null;
#[ORM\ManyToOne(targetEntity: ProfileType::class, inversedBy: 'UserProfileMatch')]
#[ORM\JoinColumn(nullable: false)]
private ?ProfileType $profileType = null;
// getts and setters
}
?>
They both have a relationship with ProfileTYpe Entity:
<?php
#[ORM\Entity(repositoryClass: ProfileTypeRepository::class)]
#[ORM\Table(name: '`profile_type`')]
class ProfileType
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 50, unique: true)]
private ?string $option = null;
public function getId(): ?int
// getters and setters
}
?>
The above defines the following relationships:
Profile entity has one associated ProfileType entity Profile may have multiple UserProfileMatch entities associated with it UserProfileMatch may have one or more associated ProfileTypes
There's no issues so far, I can do the following without issues:
<?php
$profile = new Profile();
$profile->setUserId($user->getId());
$profileType = $this->profileTypeRepository->find($formData['profileTypeId']);
$this->entityManager->persist($profile);
$this->entityManager->flush();
?>
Data is saved as expected.
But then I expand the code by adding the following code immediately after it:
<?php
$this->userProfileMatchRepository->deleteAllByProfileId($user->getId());
$this->entityManager->clear();
$this->entityManager->flush();
foreach ($formData['match_profile_type_id'] as $profileTypeId) {
$profileType = $this->profileTypeRepository->find($profileTypeId);
$userProfileMatch = new UserProfileMatch();
$userProfileMatch->setProfile($profile);
$userProfileMatch->setProfileId($profile->getId());
$userProfileMatch->setProfileType($profileType);
$userProfileMatch->setProfileTypeId($profileTypeId);
$this->entityManager->persist($userProfileMatch);
}
$this->entityManager->flush();
?>
And I get the following error:
new entity was found through the relationship 'App\Entity\Profile#profileType' that was not configured to cascade persist operations for entity: App\Entity\ProfileType@539. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\ProfileType#__toString()' to get a clue.
I only get the above error when trying to save the UserProfileMatch entity. I don't think setting cascade for the profile_type associated is ideal as the profile_type table should never be updated.
What am I doing wrong here, please help.
Upvotes: 1
Views: 29