There are situations when using MODx documents and custom TVs to store your data is not the preferred or ideal way:
- Too much processing power is used for aggregating (Ditto) and the site slows down.
- You have to import existing data from an old site or external application.
- You will most likely end up with several hundred or thousands of datasets.
- You need to do something with that data which is not possible using only the default MODx tools or the snippets/plugins available in the repository.
In such cases, it's wise to just store your data in your own mySQL table(s).
Problem: MODx doesn't have an in-built DB table-editor, but your client expects to handle everything inside MODx, i.e. using just one login, one admin-URL, one app.
Solution: Install a database table editor script as a module.
Here's how:
a) Download phpMyEdit:
http://www.phpmyedit.org/Make sure you also download the documentation package.
b) Upload it to your folder (e.g. in the manager folder: manager/phpMyEdit/)
Hint: If you're also using mySQL SETS, it's wise to fix a small typo in the phpMyEdit.class.php file:
see this forum thread for infos:
http://platon.sk/forum/projects/viewtopic.php?t=7892&highlight=set+bugc) Go to
http://www.domain.tld/manager/phpMyEdit/phpMyEditSetup.php to create a new editor-page for your DB-table.
Enter your DB connection data (hostname, username, password, DB-name, table-name) and hit "submit".
On the next page, select the identifier field. Usually this is "id" and will be preselected already.
On the third page, you can add a few options (title, default CSS etc.)
The last page presents you with the generated PHP code. The app will try to create the file for you. In case that isn't possible for some reason, just create your own file and paste the code you see on screen.
d) Now you can see the page in action at:
http://www.domain.tld/manager/phpMyEdit/filename.phpChange the CSS to your liking and/or add the manager CSS, e.g.
<link rel="stylesheet" type="text/css" href="media/style/MODxLight/style.css" />
e) Create a new module for your DB-table. Go to Modules > Manage Modules and click "New Module".
All you really need to do is add one line in the module code:
include_once $modx->config['base_path'] . "manager/phpMyEdit/filename.php";
Save it, and then reload the upper tabs-frame to see the newly created modules tab.
Hover over the new tab to see the URL: We want to see which module id we got.
e.g.
http://localhost/modx-0961p2/manager/index.php?a=112&id=4We need this for step f.
f) Add the following lines to your editor-page:
$opts['page_name'] = 'index.php';
$opts['cgi']['persist'] = array(
'id' => 4,
'a' => 112
);
Make sure the id value is the same as your module id you just created.
The var a should stay 112 = manager action "run module".
This will make sure the form-actions will always go to the correct URL.
g) Secure the page: Add this at the top of the editor-file:
<?php
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
exit('This file can not be accessed directly.');
}
if(IN_MANAGER_MODE!="true") die("Please use the MODx Manager.");
?>
This will make sure you can only access it via the manager module, not directly (remember: just "hiding" a doc has nothing to do with security).
h) Everything should work nicely now. There's a lot of configuration options to choose from. Change permissions (view, delete, create, add), default number of records per page and much much more.
e.g. $opts['inc'] = 25; // show 25 records per page (default = 15)
The documentation is really good and there's plenty of examples.
I know phpMyEdit is not the only script you could use. I've been using TableEditor for some projects in the past:
http://www.phpguru.org/static/TableEditor.htmlSetup requires a bit more work, but see for yourself what tool suits you best. Here's a feature comparison:
http://www.phpguru.org/static/TableEditorComparison.htmlNone of these scripts can replace phpMyAdmin imho, but they're also not really meant to.
I hope this little "how-to" helps someone. Adding a new module as described here takes only 5 minutes. Of course, designing the DB-table and writing queries to output data for your specific application is outside the scope of this little write-up.