-
-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplification on ORM SearchFilter for associations
- Loading branch information
Showing
6 changed files
with
228 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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\Issue5735; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ApiResource( | ||
operations : [new Get(), new GetCollection()], | ||
routePrefix: '/issue5735' | ||
)] | ||
#[ORM\Entity] | ||
#[ORM\Table(name: 'issue5735_group')] | ||
class Group | ||
{ | ||
#[ApiProperty(readable: false, writable: false, identifier: false)] | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ApiProperty(writable: false, identifier: true)] | ||
#[ORM\Column(type: 'symfony_uuid', unique: true)] | ||
private Uuid $uuid; | ||
|
||
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'groups')] | ||
private Collection $users; | ||
|
||
public function __construct() | ||
{ | ||
$this->users = new ArrayCollection(); | ||
} | ||
|
||
public function getUuid(): Uuid | ||
{ | ||
return $this->uuid; | ||
} | ||
|
||
public function setUuid(Uuid $uuid): void | ||
{ | ||
$this->uuid = $uuid; | ||
} | ||
|
||
public function getUsers(): Collection | ||
{ | ||
return $this->users; | ||
} | ||
|
||
public function addUser(User $user): void | ||
{ | ||
$this->users->add($user); | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?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\Issue5735; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; | ||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ApiResource( | ||
operations: [ | ||
new Get(), | ||
new GetCollection(), | ||
], | ||
routePrefix: '/issue5735' | ||
)] | ||
#[ApiFilter(SearchFilter::class, properties: ['groups' => 'exact'])] | ||
#[ORM\Entity] | ||
#[ORM\Table(name: 'issue5735_user')] | ||
class User | ||
{ | ||
#[ApiProperty(readable: false, writable: false, identifier: false)] | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ApiProperty(writable: false, identifier: true)] | ||
#[ORM\Column(type: 'symfony_uuid', unique: true)] | ||
private Uuid $uuid; | ||
|
||
#[ORM\ManyToMany(targetEntity: Group::class, mappedBy: 'users')] | ||
private Collection $groups; | ||
|
||
public function __construct() | ||
{ | ||
$this->groups = new ArrayCollection(); | ||
$this->uuid = Uuid::v4(); | ||
} | ||
|
||
public function getUuid(): Uuid | ||
{ | ||
return $this->uuid; | ||
} | ||
|
||
public function setUuid($uuid): void | ||
{ | ||
$this->uuid = $uuid; | ||
} | ||
|
||
public function getGroups(): Collection | ||
{ | ||
return $this->groups; | ||
} | ||
|
||
public function addGroup(Group $group): void | ||
{ | ||
$this->groups->add($group); | ||
if (!$group->getUsers()->contains($this)) { | ||
$group->addUser($this); | ||
} | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
} |