Search code examples
phpzend-framework2laminas-api-tools

Apigility code connected service across two tables / entities


I have been having trouble finding any up to date tutorials on Apigility - and nothing when it comes to writing a code connected service across multiple tables.

Say I have two tables, one to manage blog categories, the other to manage blog entries...

Category Table
+------------------+---------+
| CategoryID       | int     |
| CategoryName     | varchar |
| Tags             | varchar |
+------------------+---------+

Blog Table
+------------------+---------+
| BlogID           | int     |
| CategoryID       | int     |
| Title            | varchar |
| BlogText         | text    |
+------------------+---------+

Now I'd like to create a rest service like so...

/blog/:id

Which should return an entity like so...

BlogID
Title
BlogText
CategoryID
CategoryName
Tags

Can someone please provide me with some sample code, an example, a tutorial, something that is current that I can use with stock Apigility (not using Doctrine, etc).

When using Apigility, it creates 4 stub classes - how can these be filled to make it just work?

Thanks


Solution

  • You need to create a code-connected resource (api service) and fill up the Resource class yourself to handle GET, POST, DELETE, etc, yourself.

    The best 2 advices I can give on ZF2 related tasks:

    • Use a debugger
    • Read the source code, read the tests

    While digging through Apigility source code, I learned that a DB-Connected resource is in fact a pre-configured Code-Connected resource. So here is an example Code-Connected resource that you can get started with: https://github.com/zfcampus/zf-apigility/blob/master/src/DbConnectedResource.php

    You only have to extend it to work with multiple tables.