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

allow ManyToMany associations with referenced column name different from 'id' #631

Open
wants to merge 3 commits into
base: 1.x
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/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
foreach (self::getRelationToSourceKeyColumns($assoc) as $sourceKeyJoinColumn => $sourceKeyColumn) {
$whereId[] = "{$sourceKeyJoinColumn} = ?";

$reflField = $classMetadata->reflFields['id'];
$reflField = $classMetadata->getSingleIdReflectionProperty();
\assert(null !== $reflField);

$values[] = $reflField->getValue($entity);
Expand Down Expand Up @@ -1110,7 +1110,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
)) {
foreach ($targetAssoc['relationToTargetKeyColumns'] as $targetKeyJoinColumn => $targetKeyColumn) {
$whereId[] = "{$targetKeyJoinColumn} = ?";
$reflField = $classMetadata->reflFields['id'];
$reflField = $classMetadata->getSingleIdReflectionProperty();
\assert(null !== $reflField);
$values[] = $reflField->getValue($entity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Sonata\EntityAuditBundle\Tests\Fixtures\Relation;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class ManyToManyMultipleRelationshipCustomIdNamedEntity extends ManyToManyMultipleRelationshipEntity
{
/**
* @var int|null
*/
#[ORM\Id]
#[ORM\Column(name: 'strange_owned_id_name', type: Types::INTEGER)]
#[ORM\GeneratedValue]
protected $id;
}
37 changes: 36 additions & 1 deletion tests/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\DataLegalEntity;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\DataPrivateEntity;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\FoodCategory;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\ManyToManyMultipleRelationshipCustomIdNamedEntity;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\ManyToManyMultipleRelationshipEntity;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\ManyToManyMultipleTargetEntity;
use Sonata\EntityAuditBundle\Tests\Fixtures\Relation\OneToOneAuditedEntity;
Expand Down Expand Up @@ -67,6 +68,7 @@ final class RelationTest extends BaseTest
DataLegalEntity::class,
DataPrivateEntity::class,
DataContainerEntity::class,
ManyToManyMultipleRelationshipCustomIdNamedEntity::class,
ManyToManyMultipleRelationshipEntity::class,
ManyToManyMultipleTargetEntity::class,
];
Expand All @@ -91,6 +93,7 @@ final class RelationTest extends BaseTest
DataLegalEntity::class,
DataPrivateEntity::class,
DataContainerEntity::class,
ManyToManyMultipleRelationshipCustomIdNamedEntity::class,
ManyToManyMultipleRelationshipEntity::class,
ManyToManyMultipleTargetEntity::class,
];
Expand Down Expand Up @@ -379,12 +382,24 @@ public function testManyToManyMultipleRelationshipSameTargetEntity(): void
$manyToMany->addPrimaryTarget($targetOne);
$manyToMany->addSecondaryTarget($targetTwo);

$targetThree = new ManyToManyMultipleTargetEntity();
$targetFour = new ManyToManyMultipleTargetEntity();

$manyToManyWithCustomNameOfIdColumn = new ManyToManyMultipleRelationshipCustomIdNamedEntity();
$manyToManyWithCustomNameOfIdColumn->setTitle('manyToMany#2');
$manyToManyWithCustomNameOfIdColumn->addPrimaryTarget($targetThree);
$manyToManyWithCustomNameOfIdColumn->addSecondaryTarget($targetFour);

$em->persist($targetOne);
$em->persist($targetTwo);
$em->persist($targetThree);
$em->persist($targetFour);
$em->persist($manyToMany);
$em->persist($manyToManyWithCustomNameOfIdColumn);

$em->flush(); // #1
$em->flush(); // #1 + #2

// manyToMany#1
$manyToManyId = $manyToMany->getId();
static::assertNotNull($manyToManyId);

Expand All @@ -399,6 +414,26 @@ public function testManyToManyMultipleRelationshipSameTargetEntity(): void
// ensure that there is an audited entry for the secondaryTargets property
static::assertInstanceOf(Collection::class, $audited->getSecondaryTargets());
static::assertCount(1, $audited->getSecondaryTargets());

// manyToMany#2
$manyToManyId = $manyToManyWithCustomNameOfIdColumn->getId();
static::assertNotNull($manyToManyId);

$audited = $auditReader->find(
ManyToManyMultipleRelationshipCustomIdNamedEntity::class,
$manyToManyId,
1
);

static::assertNotNull($audited);

// ensure that there is an audited entry for the primaryTargets property
static::assertInstanceOf(Collection::class, $audited->getPrimaryTargets());
static::assertCount(1, $audited->getPrimaryTargets());

// ensure that there is an audited entry for the secondaryTargets property
static::assertInstanceOf(Collection::class, $audited->getSecondaryTargets());
static::assertCount(1, $audited->getSecondaryTargets());
}

/**
Expand Down
Loading