symfony使用api platform快速搭建API服务(2)IRI关联 langziyang Symfony博客 297 views 上一节我们添加了一个分类实体,所有CRUD接口都暴露了出来,并且我们提交了几个分类,你可以同时测试其它几个接口,甚至是删除,完全没有问题。 可是,如果我们现在只想对该实体进行增、查操作怎么办呢?我们只需要修改一下src/Entity/Category.php文件: ```php #[ApiResource( operations: [ new GetCollection(), new Get(), new Post() ] )] class Category { } ``` 这个时候刷新API文档就可以看到只有三个接口路由了,我们再测试删除,会提示路由找不到。 在api platform中,所有操作都是在 operations中定义的。包括自定义逻辑、路由等。 我们再生成一个book实体, ```php php bin/console make:entity Class name of the entity to create or update (e.g. OrangeElephant): > Book Mark this class as an API Platform resource (expose a CRUD API for it) (yes/no) [no]: > yes created: src/Entity/Book.php created: src/Repository/BookRepository.php Entity generated! Now let's add some fields! You can always add more fields later manually or by re-running this command. New property name (press <return> to stop adding fields): > name Field type (enter ? to see all types) [string]: > Field length [255]: > Can this field be null in the database (nullable) (yes/no) [no]: > updated: src/Entity/Book.php Add another property? Enter the property name (or press <return> to stop adding fields): > category Field type (enter ? to see all types) [string]: > relation What class should this entity be related to?: > Category What type of relationship is this? ------------ --------------------------------------------------------------------- Type Description ------------ --------------------------------------------------------------------- ManyToOne Each Book relates to (has) one Category. Each Category can relate to (can have) many Book objects. OneToMany Each Book can relate to (can have) many Category objects. Each Category relates to (has) one Book. ManyToMany Each Book can relate to (can have) many Category objects. Each Category can also relate to (can also have) many Book objects. OneToOne Each Book relates to (has) exactly one Category. Each Category also relates to (has) exactly one Book. ------------ --------------------------------------------------------------------- Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]: > ManyToOne Is the Book.category property allowed to be null (nullable)? (yes/no) [yes]: > no Do you want to add a new property to Category so that you can access/update Book objects from it - e.g. $category->getBooks()? (yes/no) [yes]: > A new property will also be added to the Category class so that you can access the related Book objects from it. New field name inside Category [books]: > Do you want to activate orphanRemoval on your relationship? A Book is "orphaned" when it is removed from its related Category. e.g. $category->removeBook($book) NOTE: If a Book may *change* from one Category to another, answer "no". Do you want to automatically delete orphaned App\Entity\Book objects (orphanRemoval)? (yes/no) [no]: > yes updated: src/Entity/Book.php updated: src/Entity/Category.php Add another property? Enter the property name (or press <return> to stop adding fields): > cover Field type (enter ? to see all types) [string]: > Field length [255]: > Can this field be null in the database (nullable) (yes/no) [no]: >yes updated: src/Entity/Book.php Add another property? Enter the property name (or press <return> to stop adding fields): > createdAt Field type (enter ? to see all types) [datetime_immutable]: > Can this field be null in the database (nullable) (yes/no) [no]: > updated: src/Entity/Book.php Add another property? Enter the property name (or press <return> to stop adding fields): > Success! Next: When you're ready, create a migration with php bin/console make:migration ``` 在这个实体里,我们把category字段与Category实体进行了多对一的关联,表示一个分类可以有多本书,而一本书只有一个分类,当然现实生活中一本书可以有多个分类,即多对多关系,这里我们不要去理会 记得要更新数据库,同样的在api文档里可以看到所有CRUD接口都自动生成了。同样的我们先要添加书籍: ```shell curl -X 'POST' \ 'http://192.168.68.100:8001/api/books' \ -H 'accept: application/ld+json' \ -H 'Content-Type: application/ld+json' \ -d '{ "name": "Symfony开发入门", "category": "/api/categories/1", "createdAt": "2023-09-17T02:02:18.675Z" }' ``` 返回: ``` { "@context": "/api/contexts/Book", "@id": "/api/books/1", "@type": "Book", "id": 1, "name": "Symfony开发入门", "category": "/api/categories/1", "createdAt": "2023-09-17T02:02:18+08:00" } ``` 可以看到我们在添加时,category值为"/api/categories/1",这就是上一节讲的IRI 而我们看到返回的信息中category同样返回的也是IRI,但是现实例子中,我们在取一本书时,可能想同时看到这本书的分类名称而不是IRI。下一节我们会讲这个问题 帮助PHPZlc项目! 与任何开源项目一样, 贡献代码 或 文档 是最常见的帮助方式, 但我们也有广泛的 赞助机会。 2 赞赏 加入技术群 评论 去登录