We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14162
    • 67 Posts
    I'm using the excellent modRest class, and it's working perfectly for custom tables on the core MODX database (largely thanks to Bob's Guides).

    So, for example, I can access modx_custom_table_widgets through a GET request to /api/Widgets and see a list of Widgets.

    But, I now wish to use another database. This already works fine if I run a simple connector that creates a new instance of XPDO (called e.g. 'custom_db'):

    $path = $modx->getOption('core_path').'components/';
    
    require $path.'custom_db/config/config.inc.php';
    		
    $custom_db = new xPDO($dsn, $database_user, $database_password);
    		
    $success = $custom_db->addPackage('custom_db',$path . 'custom_db/model/','custom_db_'); 


    and then call functions like:
    $custom_db->getCollection('MoreWidgets')


    But now I would like to access this database through the modRest class.

    My guess is that the modRest class is entirely fixed to the Modx object and therefore not adaptable to a different XPDO object - but I would really appreciate input from anyone. Is it possible to extend modRest to do this?

    This question has been answered by BobRay. See the first response.

    [ed. note: jimbob72 last edited this post 8 years, 7 months ago.]
    • discuss.answer
      • 3749
      • 24,544 Posts
      After looking at the code, I would say no to extending it. You would have to override most of the functions, since $modx is passed to their constructors.

      Although $modx is passed to the constructor, it isn't actually used for much of anything I can see other than the lexicon and one fromJSON call (easily replaced by json_decode).

      Rather than extending it, I would just copy & rename the file(s), change the class name, and rewrite the code to take out the $modx variable.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 14162
        • 67 Posts
        Thank you very much Bob - that confirms my view. On balance, duplicating the code might lead me down a bit of a cul de sac - given the lack of updates etc. And keeping my custom tables in the core database is not a show stopper - just a bit of a housekeeping niggle. I'll get the native API fully up to speed then consider the option of customising.