Skip to content

Commit

Permalink
Fixed referencedColumnName value to be able to use values with Embedd…
Browse files Browse the repository at this point in the history
…ed attributes
  • Loading branch information
krixer authored and ternel committed Jan 8, 2024
1 parent 87c4df6 commit 6171a43
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EventSubscriber/TranslatableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function mapTranslation(ClassMetadataInfo $classMetadataInfo, ObjectMana
/** @var ClassMetadataInfo $classMetadata */
$classMetadata = $objectManager->getClassMetadata($targetEntity);

$singleIdentifierFieldName = $classMetadata->getSingleIdentifierFieldName();
$singleIdentifierColumnName = $classMetadata->getSingleIdentifierColumnName();

$classMetadataInfo->mapManyToOne([
'fieldName' => 'translatable',
Expand All @@ -132,7 +132,7 @@ private function mapTranslation(ClassMetadataInfo $classMetadataInfo, ObjectMana
'fetch' => $this->translationFetchMode,
'joinColumns' => [[
'name' => 'translatable_id',
'referencedColumnName' => $singleIdentifierFieldName,
'referencedColumnName' => $singleIdentifierColumnName,
'onDelete' => 'CASCADE',
]],
'targetEntity' => $targetEntity,
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/Entity/TranslatableEmbeddableUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
use Doctrine\ORM\Mapping\Id;
use Ramsey\Uuid\Uuid;

#[Embeddable]
class TranslatableEmbeddableUuid
{
#[Id]
#[Column(name: 'uuid', unique: true)]
protected string $value;

public function __construct(string $value)
{
$this->value = $value;
}

public static function random(): self
{
return new self(Uuid::uuid4()->toString());
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/Entity/TranslatableEmbeddedIdentifierEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping\Embedded;
use Doctrine\ORM\Mapping\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

#[Entity]
class TranslatableEmbeddedIdentifierEntity implements TranslatableInterface
{
use TranslatableTrait;

#[Embedded(class: TranslatableEmbeddableUuid::class, columnPrefix: false)]
private TranslatableEmbeddableUuid $uuid;

public function __construct()
{
$this->uuid = TranslatableEmbeddableUuid::random();
}

/**
* @param mixed[] $arguments
* @return mixed
*/
public function __call(string $method, array $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}

public function getUuid(): TranslatableEmbeddableUuid
{
return $this->uuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Knp\DoctrineBehaviors\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embedded;
use Doctrine\ORM\Mapping\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

#[Entity]
class TranslatableEmbeddedIdentifierEntityTranslation implements TranslationInterface
{
use TranslationTrait;

#[Embedded(class: TranslatableEmbeddableUuid::class, columnPrefix: false)]
private TranslatableEmbeddableUuid $uuid;

#[Column(type: 'string')]
private ?string $title = null;

public function __construct()
{
$this->uuid = TranslatableEmbeddableUuid::random();
}

public function getUuid(): TranslatableEmbeddableUuid
{
return $this->uuid;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(string $title): void
{
$this->title = $title;
}
}
22 changes: 22 additions & 0 deletions tests/ORM/Translatable/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Knp\DoctrineBehaviors\Tests\Fixtures\Contract\Translatable\TranslatableEntityWithCustomInterface;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableCustomIdentifierEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableCustomizedEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableEmbeddedIdentifierEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableEntity;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\TranslatableEntityTranslation;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translation\TranslatableCustomizedEntityTranslation;
Expand Down Expand Up @@ -76,6 +77,27 @@ public function testShouldPersistWithCustomIdentifier(): void
$this->assertSame('awesome', $translatableEntity->translate('en')->getTitle());
}

public function testShouldPersistWithEmbeddedIdentifier(): void
{
$translatableEntity = new TranslatableEmbeddedIdentifierEntity();
$translatableEntity->translate('en')
->setTitle('awesome');
$translatableEntity->mergeNewTranslations();

$this->entityManager->persist($translatableEntity);
$this->entityManager->flush();

$uuid = $translatableEntity->getUuid();
$this->entityManager->clear();

/** @var TranslatableEntity $translatableEntity */
$translatableEntity = $this->entityManager->getRepository(TranslatableEmbeddedIdentifierEntity::class)->find(
$uuid
);

$this->assertSame('awesome', $translatableEntity->translate('en')->getTitle());
}

public function testShouldFallbackCountryLocaleToLanguageOnlyTranslation(): void
{
$translatableEntity = new TranslatableEntity();
Expand Down

0 comments on commit 6171a43

Please sign in to comment.