-
-
Notifications
You must be signed in to change notification settings - Fork 913
fix: search on nested sub-entity that doesn't use "id" as its ORM id #5623
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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\ApiResource\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; | ||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\MainResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get(uriTemplate: '/dummy_with_subresource/{id}', uriVariables: ['id']), | ||
new GetCollection(uriTemplate: '/dummy_with_subresource'), | ||
], | ||
provider : MainResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummyWithSubEntity::class) | ||
)] | ||
#[ApiFilter(SearchFilter::class, properties: ['subEntity'])] | ||
class MainResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
public string $name; | ||
public SubResource $subResource; | ||
mrossard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummySubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\SubResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get( | ||
uriTemplate: '/dummy_subresource/{strId}', | ||
uriVariables: ['strId'] | ||
), | ||
], | ||
provider: SubResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummySubEntity::class) | ||
)] | ||
class SubResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public string $strId; | ||
|
||
public string $name; | ||
} |
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; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummySubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'string')] | ||
private string $strId; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(inversedBy: 'subEntity', cascade: ['persist'])] | ||
private ?DummyWithSubEntity $mainEntity = null; | ||
|
||
public function __construct($strId, $name) | ||
{ | ||
$this->strId = $strId; | ||
$this->name = $name; | ||
} | ||
|
||
public function getStrId(): string | ||
{ | ||
return $this->strId; | ||
} | ||
|
||
public function getMainEntity(): ?DummyWithSubEntity | ||
{ | ||
return $this->mainEntity; | ||
} | ||
|
||
public function setMainEntity(DummyWithSubEntity $mainEntity): void | ||
{ | ||
$this->mainEntity = $mainEntity; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummyWithSubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(mappedBy: 'mainEntity', cascade: ['persist'], fetch: 'EAGER')] | ||
private ?DummySubEntity $subEntity = null; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getSubEntity(): ?DummySubEntity | ||
{ | ||
return $this->subEntity; | ||
} | ||
|
||
public function setSubEntity(?DummySubEntity $subEntity): void | ||
{ | ||
if (null !== $subEntity && $subEntity->getMainEntity() !== $this) { | ||
$subEntity->setMainEntity($this); | ||
} | ||
|
||
$this->subEntity = $subEntity; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\State\Issue5605; | ||
|
||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\MainResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\SubResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; | ||
|
||
class MainResourceProvider implements ProviderInterface | ||
mrossard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function __construct(private readonly ProviderInterface $itemProvider, private readonly ProviderInterface $collectionProvider) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
if ($operation instanceof Get) { | ||
/** | ||
* @var DummyWithSubEntity $entity | ||
*/ | ||
$entity = $this->itemProvider->provide($operation, $uriVariables, $context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still want to check why this is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a simple explanation is that entityClass gives us the link from the ApiResource to the entity, but nothing really tells us how to convert an entity to a resource, so when the framework gets a subentity it doesn't have a way to know which resource it's supposed to convert it to if you don't do it explicitly in a custom provider. |
||
|
||
return $this->getResource($entity); | ||
} | ||
$resources = []; | ||
$entities = $this->collectionProvider->provide($operation, $uriVariables, $context); | ||
foreach ($entities as $entity) { | ||
$resources[] = $this->getResource($entity); | ||
} | ||
|
||
return $resources; | ||
} | ||
|
||
protected function getResource(DummyWithSubEntity $entity): MainResource | ||
{ | ||
$resource = new MainResource(); | ||
$resource->name = $entity->getName(); | ||
$resource->id = $entity->getId(); | ||
$resource->subResource = new SubResource(); | ||
$resource->subResource->name = $entity->getSubEntity()->getName(); | ||
$resource->subResource->strId = $entity->getSubEntity()->getStrId(); | ||
|
||
return $resource; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.