Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed referencedColumnName to be able to use Embedded values #744

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/EventSubscriber/TranslatableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,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 @@ -135,7 +135,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