Foreign Databases
Last updated Jun 6th, 2019 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$290 per month—let's make that $500!
Learn moreUsing Foreign Databases¶
Added in 1.1
A foreign database means a another or third party database. Example: you probably have the database modx as your MODX db, and maybe you have crm for your CRM db. crm would be the foreign database.
As of version 1.1, to generate files, your foreign database needs to be on the same machine as your MODX DB and in MySQL. The database user that MODX is using must also have permissions to the foreign database.
When you run the CMPGenerator fill in the database name and if needed the table prefix. Now when you create a snippet you will need to create a new instance of xPDO and then you can code as normal (using your $foreignDB reference instead of $modx of course). See the example code below.
Basic Snippet Code Example¶
Example code¶
Example of schema and foreign DB from James Ehly - http://devtrench.com/posts/first-impressions-of-xpdo-wordpress-to-modx-migration-tool - http://devtrench.com/posts/wordpress-to-modx-migration-part-2-schema-relationships-and-comments - http://devtrench.com/posts/wordpress-to-modx-migration-part-3-templates-categories-and-postmeta
Advanced Snippet Code Example¶
The above code will create a new connection for each snippet call. So if you have 2 or 3 snippet calls to your snippet that is using a foreign db it will lag. So I wrote a simple class that will save you db connection so you don't have to reconnect each time.
foreignconnect.class.php
Now create a config file that can be required for each snippet call:
Custom¶
Then the first few lines of the snippet will looks like this instead:
Snippet¶
See Also¶
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$290 per month—let's make that $500!
Learn more