grzegorz-jamroz / sf-doctrine-api-bundle
此包的最新版本(v6.3.0)没有提供许可信息。
该Bundle为Symfony Doctrine Api提供基本功能
v6.3.0
2023-12-14 17:19 UTC
Requires
- doctrine/doctrine-bundle: ^2.11
- doctrine/orm: ^2.17
- grzegorz-jamroz/foundations: ^0.0.3
- grzegorz-jamroz/sf-api-bundle: ^6.2.2
- grzegorz-jamroz/sf-api-foundation: ^6.2
- ramsey/uuid-doctrine: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.40
- grzegorz-jamroz/filesystem: ^0.1
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5
- symfony/dotenv: ^6.0|^7.0
- symfony/yaml: ^6.0|^7.0
README
该Bundle为Symfony Doctrine Api提供基本功能
安装
composer require grzegorz-jamroz/sf-doctrine-api-bundle
- 在项目中更新路由配置
# config/routes.yaml controllers: resource: ../src/Controller/ type: attribute # ... # add those lines: ifrost_doctrine_api_controllers: resource: ../src/Controller/ type: doctrine_api_attribute # ...
- 配置Doctrine以将UUID存储为二进制字符串
# config/packages/doctrine.yaml doctrine: dbal: types: uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType # Uncomment if using doctrine/orm <2.8 # mapping_types: # uuid_binary: binary
注意: 可以配置Doctrine以以不同的方式存储UUID - 您可以在这里了解相关信息。请注意,该Bundle仅与以二进制类型存储的UUID一起工作。
- 创建实现 EntityInterface 的实体,在这种情况下,通过将
@Column
类型设置为uuid_binary
来注解属性,并定义自定义的Ramsey\Uuid\Doctrine\UuidV7Generator
生成器。
示例
<?php // src/Entity/Product.php namespace App\Entity; use App\Repository\ProductRepository; use Doctrine\ORM\Mapping as ORM; use Ifrost\DoctrineApiBundle\Entity\EntityInterface; use PlainDataTransformer\Transform; use Ramsey\Uuid\Doctrine\UuidV7Generator; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product implements EntityInterface { #[ORM\Id] #[ORM\Column(type: "uuid_binary", unique: true)] #[ORM\GeneratedValue(strategy: "CUSTOM")] #[ORM\CustomIdGenerator(class: UuidV7Generator::class)] private UuidInterface $uuid; #[ORM\Column(length: 255)] private string $name; public function __construct( UuidInterface $uuid, string $name, ) { $this->uuid = $uuid; $this->name = $name; } public function getUuid(): UuidInterface { return $this->uuid; } public function getName(): string { return $this->name; } public static function getTableName(): string { return 'product'; } /** * @return array<int, string> */ public static function getFields(): array { return array_keys(self::createFromArray([])->getWritableFormat()); } public function getWritableFormat(): array { return [ 'uuid' => $this->uuid->getBytes(), 'name' => $this->name, ]; } public function jsonSerialize(): array { return [ 'uuid' => (string) $this->uuid, 'name' => $this->name, ]; } public static function createFromArray(array $data): static|self { return new self( $data['uuid'] ?? Uuid::uuid7(), Transform::toString($data['name'] ?? ''), ); } public static function createFromRequest(array $data): static|self { return new self( $data['uuid'] === null ? Uuid::uuid7() : Uuid::fromString($data['uuid']), Transform::toString($data['name'] ?? ''), ); } }
- 创建您的控制器
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use Ifrost\ApiFoundation\Attribute\Api; use Ifrost\DoctrineApiBundle\Controller\DoctrineApiController; #[Api(entity: Product::class, path: 'products')] class ProductController extends DoctrineApiController { }
- 现在您可以调试路由。运行命令
php bin/console debug:router
您应该得到以下输出
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
products_create POST ANY ANY /products
products_update PUT ANY ANY /products/{uuid}
products_modify PATCH ANY ANY /products/{uuid}
products_delete DELETE ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------
更多自定义使用
如果您决定想要更改特定路由的路由配置,只需添加具有新参数的 Route
属性。例如
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use Ifrost\ApiFoundation\Attribute\Api; use Ifrost\DoctrineApiBundle\Controller\DoctrineApiController; use Symfony\Component\Routing\Annotation\Route; #[Api( entity: Product::class, path: 'products' )] class ProductController extends DoctrineApiController { #[Route('/create_products', name: 'products_create', methods: ['POST'])] public function create(): Response { return $this->getApi()->create(); } }
现在从 php bin/console debug:router
生成的输出将是
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_create POST ANY ANY /create_products
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
products_update PUT ANY ANY /products/{uuid}
products_modify PATCH ANY ANY /products/{uuid}
products_delete DELETE ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------
可以禁用某些操作。在这种情况下,可以使用 excludedActions
元数据。
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\Product; use Ifrost\ApiFoundation\Attribute\Api; use Ifrost\DoctrineApiBundle\Controller\DoctrineApiController; #[Api( entity: Product::class, path: 'products', excludedActions: [ Action::CREATE, Action::UPDATE, Action::MODIFY, 'delete', 'not_valid_actions_will_be_omitted' ])] )] class ProductController extends DoctrineApiController { }
现在从 php bin/console debug:router
生成的输出将是
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
products_find GET ANY ANY /products
products_find_one GET ANY ANY /products/{uuid}
------------------- -------- -------- ------ --------------------------
配置
默认配置
您可以在项目中添加 config/packages/ifrost_doctrine_api.yaml
以启用/禁用某些功能(如果不需要)
# config/packages/ifrost_doctrine_api.yaml # default config ifrost_doctrine_api: doctrine_dbal_types_uuid: true dbal_cache_adapter: enabled: false db_client: enabled: true # ...
您可以启用默认缓存适配器 Symfony\Component\Cache\Adapter\FilesystemAdapter
(可选)
# config/packages/ifrost_doctrine_api.yaml ifrost_doctrine_api: doctrine_dbal_types_uuid: true dbal_cache_adapter: enabled: true db_client: enabled: true # ...