Hi, I would like to know how I can do to relate two tables with RESTFul API (it’s my first api).
I am creating a page that displays the information directly with the content that I can find into the API that I built. for example I have a table called “resource_content” and the other one is “body”. My relation is that the “body” could have only one “resource_content”, but “resource_content” can be in any “body”, so, my relate is one to much.
<object class="ResourceContent" table="resource_content" extends="xPDOSimpleObject">
<field key="titleBlock" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="description" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="block_id" dbtype="int" phptype="integer" null="false" default=""/>
<field key="resource_id" dbtype="int" phptype="integer" null="false" default=""/>
<field key="properties" dbtype="json" phptype="json" null="true"/>
<aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
<aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
</object>
<object class="Body" table="body" extends="xPDOSimpleObject">
<field key="titleBlock" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="description" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="properties" dbtype="json" phptype="json" null="true"/>
<aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
<aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
</object>
With these objects, I created an class by every object
class MycontrollerResourceContent extends modRestController {
/** @var string $classKey The xPDO class to use */
public $classKey = 'ResourceContent';
/** @var string $defaultSortField The default field to sort by in the getList method */
public $defaultSortField = 'id';
/** @var string $defaultSortDirection The default direction to sort in the getList method */
public $defaultSortDirection = 'ASC';
}
and the other class
class MycontrollerBody extends modRestController {
/** @var string $classKey The xPDO class to use */
public $classKey = 'Body';
/** @var string $defaultSortField The default field to sort by in the getList method */
public $defaultSortField = 'id';
/** @var string $defaultSortDirection The default direction to sort in the getList method */
public $defaultSortDirection = 'ASC';
}
As I said before, I would like to relate both tables and make a request that when I call a body it comes with the resource or resources that have been assigned to it.
I don’t know if I made myself understood, I hope your help.
Thanks a lot