Search code examples
modxmodx-revolution

Separate MODx contexts storing data in separate tables from modx_site_content table


I have all my data that other external software is using in "modx_site_content" table. Everything is great - we view/add/edit/delete data into one table that is used by MODx and from 2 other PHP/JS scripts. But I would love to have another table that MODx and other scripts both use. Is it possible maybe that I create new context that will use for example "modx_site_content2" table?


Solution

  • It is possible, but not really a good way to do it. First off it would require quite lot of plugins to work out how it works. What I would try to do is (I do not have running instance). And requires a bit of fiddling with php and sql.

    Create plugin to onLoadWebDocument, check what is the context key from $modx->context->key (or something to that direction) on the plugin you have $modx->resource object which you can actually override.

    So next load an resource with the url path (you need to split it) which will match the alias of the resource using sql query. Have a php part like next:

    $res = $modx->query('your_modResource_clone', 214); // 214 is the resource id and the object needs to inherit modResource
    $obj = new modResource($modx);
    $obj->fromArray($res->toArray()); // Could be that you can actually assign the object directly to line below.
    $modx->resource = $obj;
    

    This actually should work as the object is just loaded and not rendered. The code might be bit off (like said don't have any running instances). There is also a chance that modx shall not cache it. It should but am not sure right now. Note that you also need parse the url to get right id, that'll make this more tougher. But the basic concept is there. Hopefully