Skip to content
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
38 changes: 28 additions & 10 deletions src/Doctrine/Orm/Filter/IriFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
return;
}

// Collection associations (OneToMany/ManyToMany) require a JOIN to compare individual elements.
if ($ormLeafMetadata['is_collection_valued']) {
// Collection associations (OneToMany/ManyToMany) and inverse-side OneToOne associations require a JOIN to compare individual elements.
if ($ormLeafMetadata['is_collection_valued'] || $ormLeafMetadata['is_inverse_side']) {
if (Join::LEFT_JOIN === $this->joinTypeFor($context)) {
$queryBuilder->leftJoin(\sprintf('%s.%s', $alias, $property), $parameterName);
} else {
Expand All @@ -79,12 +79,21 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
if (is_iterable($value)) {
$queryBuilder
->{$context['whereClause'] ?? 'andWhere'}(\sprintf('%s IN (:%s)', $parameterName, $parameterName));
} else {
$queryBuilder
->{$context['whereClause'] ?? 'andWhere'}(\sprintf('%s = :%s', $parameterName, $parameterName));
$queryBuilder->setParameter($parameterName, $value);

return;
}

$queryBuilder->setParameter($parameterName, $value);
$queryBuilder
->{$context['whereClause'] ?? 'andWhere'}(\sprintf('%s = :%s', $parameterName, $parameterName));

if ($ormLeafMetadata['is_collection_valued']) {
$queryBuilder->setParameter($parameterName, $value);

return;
}

$this->setIdentifierParameter($queryBuilder, $parameterName, $value, $ormLeafMetadata);

return;
}
Expand All @@ -103,8 +112,16 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
$queryBuilder
->{$context['whereClause'] ?? 'andWhere'}(\sprintf('%s = :%s', $propertyExpr, $parameterName));

// Extract the identifier value and its type from the target entity metadata
// to properly handle custom ID types (e.g. UUID).
$this->setIdentifierParameter($queryBuilder, $parameterName, $value, $ormLeafMetadata);
}

/**
* Binds the identifier of the target entity with its type, to properly handle custom ID types (e.g. UUID).
*
* @param array{association_target_class: string, identifier_type: ?string} $ormLeafMetadata
*/
private function setIdentifierParameter(QueryBuilder $queryBuilder, string $parameterName, mixed $value, array $ormLeafMetadata): void
{
$targetClass = $ormLeafMetadata['association_target_class'];
$em = $queryBuilder->getEntityManager();
$targetMetadata = $em->getClassMetadata($targetClass);
Expand Down Expand Up @@ -137,7 +154,7 @@ private function applyEnumComparison(QueryBuilder $queryBuilder, string $alias,
}

/**
* @return array{is_collection_valued: bool, association_target_class: ?string, identifier_type: ?string}|null
* @return array{is_collection_valued: bool, is_inverse_side: bool, association_target_class: ?string, identifier_type: ?string}|null
*/
private function getOrmLeafMetadata(mixed $parameter): ?array
{
Expand All @@ -159,7 +176,7 @@ private function getOrmLeafMetadata(mixed $parameter): ?array
* Returns null when the leaf is a backed enum column so the caller compares it directly;
* a non-association, non-enum leaf is left to raise a mapping error.
*
* @return array{is_collection_valued: bool, association_target_class: string, identifier_type: ?string}|null
* @return array{is_collection_valued: bool, is_inverse_side: bool, association_target_class: string, identifier_type: ?string}|null
*/
private function resolveLeafMetadataAtRuntime(QueryBuilder $queryBuilder, string $resourceClass, string $originalProperty, string $leafProperty): ?array
{
Expand Down Expand Up @@ -189,6 +206,7 @@ private function resolveLeafMetadataAtRuntime(QueryBuilder $queryBuilder, string

return [
'is_collection_valued' => $isCollectionValued,
'is_inverse_side' => $metadata->isAssociationInverseSide($leafProperty),
'association_target_class' => $targetClass,
'identifier_type' => $identifierType,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\IriFilterInverseOneToOne;

use ApiPlatform\Doctrine\Orm\Filter\ExactFilter;
use ApiPlatform\Doctrine\Orm\Filter\IriFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\State\ParameterProvider\IriConverterParameterProvider;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'iri_filter_filtered_dummy')]
#[ApiResource(
shortName: 'IriFilterFilteredDummy',
operations: [
new Get(),
new GetCollection(
paginationEnabled: false,
parameters: [
'name' => new QueryParameter(filter: new ExactFilter()),
'owningSideDummy' => new QueryParameter(filter: new IriFilter(), property: 'owningSideDummy'),
'uuidOwningSideDummy' => new QueryParameter(filter: new IriFilter(), property: 'uuidOwningSideDummy'),
'orOwningSideDummy' => new QueryParameter(filter: new OrFilter(new IriFilter()), property: 'owningSideDummy', provider: IriConverterParameterProvider::class),
]
),
]
)]
class FilteredDummy
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public ?int $id = null;

#[ORM\Column]
public string $name = '';

#[ORM\OneToOne(targetEntity: OwningSideDummy::class, mappedBy: 'filteredDummy')]
public ?OwningSideDummy $owningSideDummy = null;

#[ORM\OneToOne(targetEntity: UuidOwningSideDummy::class, mappedBy: 'filteredDummy')]
public ?UuidOwningSideDummy $uuidOwningSideDummy = null;

#[ORM\OneToOne(targetEntity: InverseSideDummy::class, inversedBy: 'filteredDummy')]
public ?InverseSideDummy $inverseSideDummy = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\IriFilterInverseOneToOne;

use ApiPlatform\Doctrine\Orm\Filter\IriFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'iri_filter_inverse_side_dummy')]
#[ApiResource(
shortName: 'IriFilterInverseSideDummy',
operations: [
new Get(),
new GetCollection(
parameters: [
'filteredDummy.owningSideDummy' => new QueryParameter(filter: new IriFilter(), property: 'filteredDummy.owningSideDummy'),
'filteredDummy.uuidOwningSideDummy' => new QueryParameter(filter: new IriFilter(), property: 'filteredDummy.uuidOwningSideDummy'),
]
),
]
)]
class InverseSideDummy
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public ?int $id = null;

#[ORM\Column]
public string $name = '';

#[ORM\OneToOne(targetEntity: FilteredDummy::class, mappedBy: 'inverseSideDummy')]
public ?FilteredDummy $filteredDummy = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\IriFilterInverseOneToOne;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'iri_filter_owning_side_dummy')]
#[ApiResource(
shortName: 'IriFilterOwningSideDummy',
operations: [new Get()]
)]
class OwningSideDummy
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public ?int $id = null;

#[ORM\OneToOne(targetEntity: FilteredDummy::class, inversedBy: 'owningSideDummy')]
#[ORM\JoinColumn(nullable: false)]
public ?FilteredDummy $filteredDummy = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\IriFilterInverseOneToOne;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity]
#[ORM\Table(name: 'iri_filter_uuid_owning_side_dummy')]
#[ApiResource(
shortName: 'IriFilterUuidOwningSideDummy',
operations: [new Get()]
)]
class UuidOwningSideDummy
{
#[ORM\Id]
#[ORM\Column(type: 'symfony_uuid', unique: true)]
public Uuid $id;

#[ORM\OneToOne(targetEntity: FilteredDummy::class, inversedBy: 'uuidOwningSideDummy')]
#[ORM\JoinColumn(nullable: false)]
public ?FilteredDummy $filteredDummy = null;

public function __construct()
{
$this->id = Uuid::v7();
}
}
Loading
Loading