I have the following entity:
class DoctrinePartnerDao extends DoctrineBaseDao
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public int $id;
#[ORM\Column(type: 'uuid', unique: true, nullable: false)]
public Uuid $guid;
#[ORM\Column(type: 'string', length: 255, nullable: false)]
public string $name;
#[
ORM\ManyToOne(
targetEntity: DoctrineCountryDao::class,
cascade: ['persist']
)
]
public DoctrineCountryDao $country;
#[ORM\ManyToOne(targetEntity: DoctrineImageDao::class)]
#[ORM\JoinColumn(name: 'banner_image_id', nullable: true)]
public ?DoctrineImageDao $bannerImage = null;
#[ORM\ManyToOne(targetEntity: DoctrineImageDao::class)]
#[ORM\JoinColumn(name: 'avatar_image_id', nullable: true)]
public ?DoctrineImageDao $avatarImage = null;
#[ORM\Column(type: 'datetime', nullable: true)]
public ?DateTimeInterface $deletedAt;
And the save method:
public function save(Partner $partner): Partner
{
/** @var DoctrinePartnerDao */
$dao = $this->mapper->toDao($partner);
$countryId = $partner->getCountryId();
$countryReference = $this->getEntityManager()->getReference(
DoctrineCountryDao::class,
$countryId
);
if (!$countryReference) {
throw new InvalidAggregateIdException(
"Country with id '$countryId' does not exist."
);
}
$bannerImageReference = $partner->getBannerImageId()
? $this->getEntityManager()->getReference(
DoctrineImageDao::class,
$partner->getBannerImageId()
)
: null;
$avatarImageReference = $partner->getAvatarImageId()
? $this->getEntityManager()->getReference(
DoctrineImageDao::class,
$partner->getAvatarImageId()
)
: null;
$dao->country = $countryReference;
$dao->bannerImage = $bannerImageReference;
$dao->avatarImage = $avatarImageReference;
try {
$this->getEntityManager()
->persist($dao)
->flush();
} catch (Exception $e) {
dd($e);
}
return $partner;
}
Expected behavior: create the entity in db if it doesn't exist and if it exists update it. I am trying to avoid finding it first.
Before it was a 'merge' method but it seems it is deprecated now.
Getting the following error: #message: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'i\x03\xE4\xED\x18\xAE8\x18\xB6%\xD1\x18\xD7D\xE6\xBA' for key 'partner.UNIQ_312B3E162B6FCFB2'"