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
33 changes: 24 additions & 9 deletions src/Doctrine/Orm/NestedPropertyHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,31 @@ protected function addNestedParameterJoins(
return [$alias, $property];
}

foreach ($nestedInfo['converted_relation_segments'] as $association) {
$alias = QueryBuilderHelper::addJoinOnce(
$queryBuilder,
$queryNameGenerator,
$alias,
$association,
$joinType
);
$relationClasses = $nestedInfo['relation_classes'] ?? [];
$association = null;
$embedded = false;

foreach ($nestedInfo['converted_relation_segments'] as $id => $association) {
$entityClass = $relationClasses[$id] ?? null;
if (!$entityClass) {
continue;
}

$embedded = !$queryBuilder->getEntityManager()->getClassMetadata($entityClass)->hasAssociation($association);

if (!$embedded) {
$alias = QueryBuilderHelper::addJoinOnce(
$queryBuilder,
$queryNameGenerator,
$alias,
$association,
$joinType
);
}
}

return [$alias, $nestedInfo['leaf_property']];
$leafProperty = $nestedInfo['leaf_property'];

return [$alias, $embedded && $association ? $association.'.'.$leafProperty : $leafProperty];
}
}
4 changes: 1 addition & 3 deletions src/Metadata/Util/ResourceClassInfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ protected function getClassNameFromProperty(ApiProperty $propertyMetadata): ?str
return null;
}

$className = $this->extractClassNameFromType($type);

return $className && $this->isResourceClass($className) ? $className : null;
return $this->extractClassNameFromType($type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\ApiResource\Issue7916;

use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7916\UserAction;

/**
* API Resource DTO for UserAction using stateOptions pattern.
* This is a separate API Resource for the UserAction entity which is NOT itself marked #[ApiResource].
*
* Tests that nested property filters work on relations to non-ApiResource entities (User).
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/user-actions',
parameters: [
'name' => new QueryParameter(
filter: new PartialSearchFilter(),
property: 'user.name',
),
'email' => new QueryParameter(
filter: new PartialSearchFilter(),
property: 'user.email',
),
],
),
],
stateOptions: new Options(entityClass: UserAction::class),
)]
class UserActionResource
{
public ?int $id = null;
public ?string $action = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\ApiResource\Issue7916;

use ApiPlatform\Doctrine\Odm\Filter\PartialSearchFilter;
use ApiPlatform\Doctrine\Odm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Issue7916\UserAction;

/**
* API Resource DTO for MongoDB UserAction using stateOptions pattern.
* This is a separate API Resource for the UserAction document which is NOT itself marked #[ApiResource].
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/user-actions',
parameters: [
'name' => new QueryParameter(
filter: new PartialSearchFilter(),
property: 'user.name',
),
'email' => new QueryParameter(
filter: new PartialSearchFilter(),
property: 'user.email',
),
],
),
],
stateOptions: new Options(documentClass: UserAction::class),
)]
class UserActionResourceOdm
{
public ?int $id = null;
public ?string $action = null;
}
34 changes: 34 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue7916/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\ApiResource\Issue7916;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7916\User;

/**
* API Resource DTO for User using stateOptions pattern.
* This is a separate API Resource for the User entity which is NOT itself marked #[ApiResource].
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ApiResource(
stateOptions: new Options(entityClass: User::class),
)]
class UserResource
{
public ?int $id = null;
public ?string $name = null;
public ?string $email = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\ApiResource\Issue7916;

use ApiPlatform\Doctrine\Odm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Issue7916\User;

/**
* API Resource DTO for MongoDB User using stateOptions pattern.
* This is a separate API Resource for the User document which is NOT itself marked #[ApiResource].
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ApiResource(
stateOptions: new Options(documentClass: User::class),
)]
class UserResourceOdm
{
public ?int $id = null;
public ?string $name = null;
public ?string $email = null;
}
66 changes: 66 additions & 0 deletions tests/Fixtures/TestBundle/Document/Issue7916/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Document\Issue7916;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* User document for issue #7916.
*
* This document is NOT marked with #[ApiResource] to test nested property filtering
* on relations to non-API-Resource entities.
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ODM\Document(collection: 'issue_7916_user')]
class User
{
#[ODM\Id(type: 'int', strategy: 'INCREMENT')]
private ?int $id = null;

#[ODM\Field(type: 'string')]
private ?string $name = null;

#[ODM\Field(type: 'string')]
private ?string $email = null;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(?string $email): self
{
$this->email = $email;

return $this;
}
}
66 changes: 66 additions & 0 deletions tests/Fixtures/TestBundle/Document/Issue7916/UserAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Document\Issue7916;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* MongoDB version of UserAction for issue #7916.
*
* This document has a reference to User (which is NOT an ApiResource).
* Used to test nested property filtering on non-ApiResource relations with PartialSearchFilter.
*
* @see https://github.com/api-platform/core/issues/7916
*/
#[ODM\Document(collection: 'issue_7916_user_action')]
class UserAction
{
#[ODM\Id(type: 'int', strategy: 'INCREMENT')]
private ?int $id = null;

#[ODM\Field(type: 'string')]
private string $action = '';

#[ODM\ReferenceOne(targetDocument: User::class, storeAs: 'id')]
private ?User $user = null;

public function getId(): ?int
{
return $this->id;
}

public function getAction(): string
{
return $this->action;
}

public function setAction(string $action): self
{
$this->action = $action;

return $this;
}

public function getUser(): ?User
{
return $this->user;
}

public function setUser(?User $user): self
{
$this->user = $user;

return $this;
}
}
Loading
Loading