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

Fix ignoring custom types for PersistentCollection matching() #9010

Merged
merged 2 commits into from
Sep 15, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$onConditions = $this->getOnConditionSQL($mapping);
$whereClauses = $params = [];
$paramTypes = [];

if (! $mapping['isOwningSide']) {
$associationSourceClass = $targetClass;
Expand All @@ -253,6 +254,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
$params[] = $ownerMetadata->containsForeignIdentifier
? $id[$ownerMetadata->getFieldForColumn($value)]
: $id[$ownerMetadata->fieldNames[$value]];
$paramTypes[] = PersisterHelper::getTypeOfColumn($value, $ownerMetadata, $this->em);
}

$parameters = $this->expandCriteriaParameters($criteria);
Expand All @@ -263,6 +265,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
$field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform);
$whereClauses[] = sprintf('te.%s %s ?', $field, $operator);
$params[] = $value;
$paramTypes[] = PersisterHelper::getTypeOfColumn($field, $targetClass, $this->em);
}

$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
Expand All @@ -281,7 +284,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri

$sql .= $this->getLimitSql($criteria);

$stmt = $this->conn->executeQuery($sql, $params);
$stmt = $this->conn->executeQuery($sql, $params, $paramTypes);

return $this
->em
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Doctrine\Tests\Models\Tweet\Tweet;
use Doctrine\Tests\Models\Tweet\User;
use Doctrine\Tests\Models\Tweet\User as TweetUser;
use Doctrine\Tests\Models\ValueConversionType\InversedManyToManyExtraLazyEntity;
use Doctrine\Tests\Models\ValueConversionType\OwningManyToManyExtraLazyEntity;
use Doctrine\Tests\OrmFunctionalTestCase;

class PersistentCollectionCriteriaTest extends OrmFunctionalTestCase
Expand All @@ -19,6 +21,7 @@ protected function setUp(): void
{
$this->useModelSet('tweet');
$this->useModelSet('quote');
$this->useModelSet('vct_manytomany_extralazy');
parent::setUp();
}

Expand Down Expand Up @@ -95,4 +98,31 @@ public function testCanCountWithoutLoadingPersistentCollection(): void
$this->assertCount(1, $tweets);
$this->assertFalse($tweets->isInitialized());
}

public function testCanHandleComplexTypesOnAssociation(): void
{
$parent = new OwningManyToManyExtraLazyEntity();
$parent->id2 = 'Alice';

$this->_em->persist($parent);

$child = new InversedManyToManyExtraLazyEntity();
$child->id1 = 'Bob';

$this->_em->persist($child);

$parent->associatedEntities->add($child);

$this->_em->flush();
$this->_em->clear();

$parent = $this->_em->find(OwningManyToManyExtraLazyEntity::class, $parent->id2);

$criteria = Criteria::create()->where(Criteria::expr()->eq('id1', 'Bob'));

$result = $parent->associatedEntities->matching($criteria);

$this->assertCount(1, $result);
$this->assertEquals('Bob', $result[0]->id1);
}
}