How To Create Dynamic Resources Based On An Updated MIGXDB Field?

Background

I have an MIGX CMP that saves data to an MIGXDB and I can successfully display the data on a single page on the front end using MIGX Loop Collection

Next Step

What I want to achieve is to dynamically create a resource (url) based on the saved data from one field:

if site.com is the home page; then when the data is saved to the database I want it to create a resource with a full url of: site.com/siteid

From the schema the important fields are:

&packageName=towersite
&classname=TowerSite
&selectfields=id,siteid

It is the content in the “siteid” field that I would like to be used as a new url for a new page every time the database is updated via the CMP.

Each dynamically created resource will use the same template which will be populated from the database using a loop collection chunk

My question is how can I create the required snippet or code to create this new resource from the database field?

I’m kinda hoping someone has done something like this before and could share a snippet/chunk of code for how to do this as I do not know

MODX Revolution 2.6.5-pl
PHP V7.3.0
MIGX 2.11
On a shared hosting environment (lynex apache with MySql)
Database version: 5.6.40-84.0-log
Database charset: utf8

1 Like

if it is only, because of FURLs you can use a plugin, which runs at onPageNotFound

For example https://modx.com/extras/package/customrequest

1 Like

Hi Bruno,

Thanks for taking the time to share a possible solution.

I have been looking at Custom Request but I cannot figure out how it can generate a new Resource using a field name (siteid) from a database field

I tried to create a custom request with:
Configuration name = Site
Alias Path = Blank
Resource = site
URI Parameter = [“siteid”]

However this did not generate a new resource (maybe I did something wrong here)

I’m not so bothered about FURL’s, so if there is any available method of achieving generation of a resource (new web page) I would definitely like to try it

1 Like

Can you explain, why you want to create a resource for every item in your db-table?

I was thinking, it was for the purpose to have a FURL for each.
For this, you don’t need resources, but could just use CustomRequest for the routing

1 Like

I may be out of place, but resources are a special thing in Modx. They are pages, if needed, but also serve other functions, and are only one way to hold and store data.

I am not sure if custom url is only possible with a new resource, but I do want you to understand that data, lots of data, can be stored and updated without using resources. Also creating resources might not be easy to do, I don’t know. But I think you can consider what you really want, is the important thing the url, or is the new resource the critical piece?

Bruno he seems to want a custom url more than anything, created (I think) on CMP update. When he says resource (url) I think he cares only about url, not resource.

What I want to achieve is to dynamically create a resource (url) based on the saved data from one field:

It is the content in the “siteid” field that I would like to be used as a new url for a new page every time the database is updated via the CMP.

@jamesjazz, do you want the url to be specific (directly from siteid db table, I think you meant) or can it be random?

It would be strange to let MIGX do such a function, you may want a snippet with php that grabs the data and makes the url…but I am out of my league here so take it all with a grain of salt

Hi Bruno & nuan88,

Maybe I not explain clearly.

“Can you explain, why you want to create a resource for every item in your db-table?”

Automation … Upon every update in the CMP there are a total of 79 fields in the schema, I want to create a URL in the resource tree for only one field in the db-table “siteid”. So upon every CMP update a new URL will be created in the resource tree. Each new resource url would be assigned to the same template

“Bruno he seems to want a custom url more than anything, created (I think) on CMP update. When he says resource (url) I think he cares only about url, not resource”

Yes, that is exactly what I am trying to do, create a single unique url on each CMP update

@jamesjazz, do you want the url to be specific (directly from siteid db table, I think you meant) or can it be random?”

I want the url to be specific to the db field “siteid” only

“you may want a snippet with php that grabs the data and makes the url”

Yes that would work, a piece of code can grab the data from that field and create a url in the resource tree is exactly what I want. Below is the data base details:

packageName=towersite
classname=TowerSite
selectfields=siteid

Unfortunately I do not know how to do this and I’m not even sure if it can be done

ok, if you really want to create a resource on update, this is possible with a aftersave - hooksnippet

Hi Bruno,

This snippet would be placed in MIGXDB-Settings, under Hook Snippets, yes?

I’ve been searching for an example code that I could adapt to achieve this but not found anything yet

some examples


http://bruno17.github.io/MIGX/tipsandtricks/
https://forums.modx.com/thread/99978/migx-custom-cmp---custom-db---updating-records-custom-table-and-some-modx-db-tables