Tutorial on adding Relationships from Page?

42 views
Skip to first unread message

John White

unread,
Jul 12, 2022, 6:18:58 PM7/12/22
to structr
I went through the existing tutorials out there on Structr, but didn't see one that incorporated create/read/update actions on the relationships between data within a Page. Is that in the docs somewhere that I'm not seeing?

Kai Schwaiger

unread,
Jul 13, 2022, 12:36:10 PM7/13/22
to str...@googlegroups.com
Hi John,

I don’t understand fully - do you want to edit relationships between Page-objects (like in the page-editor in the admin-UI)?

Or are you trying to edit relationship attributes between custom nodes?

If it is the latter I would recommend modelling those relationships as hyper-relationships because structrs security system is based on relationships to nodes and thus relationships themselves can not be security-checked.

If you really want to, you could still do it but it involves a bit of scripting to get the relationships. You can use the functions "get_incoming_relationships()” and “get_outgoing_relationships()” to find the relationships between nodes.

Then, if you’d like to simply use a REST request to update those relationships you could - you’d only need its “type” and “id” attribute and would be able to issue a PUT request to /structr/rest/<type>/<id> with the new data. (You would not be able to update the source and target node though, this action is handled by structr internally and involves deleting the relationship and creating a new one).

The “type” attribute is auto-created and should look familiar - it always looks like this: <SourceType><RelType><TargetType>

Does that help?

Best,
Kai


On 13. Jul 2022, at 00:18:58, John White <jwhi...@gmail.com> wrote:

I went through the existing tutorials out there on Structr, but didn't see one that incorporated create/read/update actions on the relationships between data within a Page. Is that in the docs somewhere that I'm not seeing?

--
You received this message because you are subscribed to the Google Groups "structr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structr+u...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/structr/a6d5fe84-4448-4905-b91c-fe6e8e1eb3dcn%40googlegroups.com.

John White

unread,
Jul 13, 2022, 3:07:23 PM7/13/22
to structr
Sorry if I wasn't that precise. I *think* I'm talking about Page-objects. For example, in the Structr blogging tutorial, there's a BlogPost object and a Category object with a schema-defined relationship. I'm not talking about creating/modifying, deleting that relationship in the schema. I'm talking about how to create/modify/delete members of that relationship.

Again, in the blog app example, how would one add the Category when creating a new BlogPost? Is it as simple as passing a Category ID (or an array of Category IDs) in the update?

If I have BlogPost.hasCategories relationship, when I create a new BlogPost, can I just include a single Category UUID in "hasCategories"? What if I allow multiple categories? 


Christian Morgner

unread,
Jul 13, 2022, 5:03:02 PM7/13/22
to str...@googlegroups.com
Hi John,

Yes, it's as simple as passing an ID (or an array of IDs, depending on the multiplicity of the relationship) in the update / create call.

See https://docs.structr.com/docs/rest-guide for more information. Structr works very much like a JSON document database when accessed via REST.

Best,
Christian


Am 13.07.22 um 21:07 schrieb John White:
>> To view this discussion on the web, visit https://groups.google.com/d/msgid/structr/a6d5fe84-4448-4905-b91c-fe6e8e1eb3dcn%40googlegroups.com <https://groups.google.com/d/msgid/structr/a6d5fe84-4448-4905-b91c-fe6e8e1eb3dcn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups "structr" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to structr+u...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/structr/3c0384d8-c2e8-4c27-b49e-e35b558490c5n%40googlegroups.com <https://groups.google.com/d/msgid/structr/3c0384d8-c2e8-4c27-b49e-e35b558490c5n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Christian Morgner
Structr GmbH · Hanauer Landstr. 291a · 60314 Frankfurt · Germany
Phone: +49 163 2536088
Twitter: @cmor_

http://structr.com

John White

unread,
Jul 16, 2022, 2:52:42 PM7/16/22
to structr
Hmmm, I was trying to use a create() from the page. Is there a simple way to mimic the Data tab's new node construction, where when adding a relationship, there's already an enumeration of the existing nodes to add an relationship to and just ... click to add?

I was thinking something like find('Category') as a repeater, then attaching each result to a selectable button. But is there an array constructor that I'm just not finding? Or can create just take a {id1, id2, id3} in the middle and read it as an array? I'm not even clear on how to go from a bootstrap multiselect (for example) to a valid create() statement that creates the node and the (multiple) relationships.

Kai Schwaiger

unread,
Jul 18, 2022, 6:35:40 AM7/18/22
to str...@googlegroups.com
Hi John,

creating a BlogPost would involve a REST POST request like this:

fetch('/structr/rest/BlogPost', {
body: JSON.stringify({
name: "My blog post",
manyCategories: [ "uuid_category_1", "uuid_category_2", "uuid_category_3" ]
})
})

Structr would automatically create the node and the relationships to the categories.

This should work like in the docs under the heading "Reference by UUID" (https://docs.structr.com/docs/rest-guide#reference-by-uuid)


Creating the data would usually be handled in JavaScript and then sent to structr via the REST interface. Is that what you meant?

HTH
Kai



https://structr.com
Kai Schwaiger
COO
kai.sc...@structr.com
Mobil
+49 151 / 419 811 76

Structr GmbH
Hanauer Landstraße 291
60314 Frankfurt am Main
Deutschland / Germany
Geschäftsführer: Axel Morgner
Sitz der Ges.: Frankfurt a. M.
HR: AG Frankfurt a. M. HRB 92262
Umsatzsteuer-ID: DE 279486092 




Reply all
Reply to author
Forward
0 new messages