Quote from: stefany at Jun 18, 2017, 09:44 PMI think MODX ships with xPDO.
Remember to post the error you get when you are testing a snippet and it doesn't work.
How do you post the errors?
The code I gave above was only to illustrate an example of using xPDO
$sql = 'SELECT * FROM `organizer_directory` '; $c = new xPDOCriteria($modx,$sql); // connect to the MODX DB $rows = array(); if ($c->stmt && $c->stmt->execute()) { while ($row = $c->stmt->fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; } } print_r($rows);
This one should help. It will probably spit out some errors since I do not know the internal organization of the DB, but you should be able to easily fix them.
The general process to start with xPDO is like this:
1. Create a schema file (XML) that describes your tables and its fields. See this section in the docs.
2. Use a build script to turn that schema file into model classes. These typically go into core/components/yourcomponent/model/yourcomponent/ and are the "compiled" version of your schema that xPDO will understand. Here's how that works, and a (random) full example is here.
3. Where you need to access your custom table(s), call $modx->addPackage('yourcomponent', 'path/to/core/components/yourcomponent/model/') - this registers your custom model files with xPDO. It's common to put this code in a service class that you load with getService instead. See here.
4. Use any of the $modx->newQuery/$modx->getObject/$modx->getCollection/$modx->getIterator/etc methods to interact with your tables. More info here.
Using the standard PDOStatements like you're doing is quicker (just feed in your query), but going through these steps give you access to everything xPDO has to offer.
I think this is a direct answer to your question. It's about how to start with a custom table and make things xPDO-friendly: https://bobsguides.com/custom-db-tables.html.
There's a script there you can download to create the schema, class, and map, files from an existing custom DB table, as well as a discussion about how to use xPDO.
$fields=$quotes->toArray(); $modx->log(modX::LOG_LEVEL_ERROR, 'FIELDS: ' . print_r($fields, true));
Is there anything in the MODX Error Log?
You can also try this:
$fields=$quotes->toArray(); $modx->log(modX::LOG_LEVEL_ERROR, 'FIELDS: ' . print_r($fields, true));
Just a note: Any time you modify the schema, you have to re-run the script. MODX ignores the schema file itself and just uses the class and map files.
MODX doesn't like PHP tags embedded in the HTML, so they may be causing trouble. The content of the $output variable should appear where the snippet tag is placed (you do have a snippet tag on the page, right?).
Quote from: BobRay at Jun 22, 2017, 03:36 PMIs there anything in the MODX Error Log?
You can also try this:
$fields=$quotes->toArray(); $modx->log(modX::LOG_LEVEL_ERROR, 'FIELDS: ' . print_r($fields, true));
Just a note: Any time you modify the schema, you have to re-run the script. MODX ignores the schema file itself and just uses the class and map files.
MODX doesn't like PHP tags embedded in the HTML, so they may be causing trouble. The content of the $output variable should appear where the snippet tag is placed (you do have a snippet tag on the page, right?).
I got your example working and displaying. Trying to convert it to my table pulling only three values. Here is my updated snippet
<!--?php
$path = MODX_CORE_PATH . 'components/directories/';
$result = $modx--->addPackage('directories',$path .
'model/','organizer_');
if (! $result) {
return 'failed to add package';
} else {
$output = '<p>added Package</p>';
}
$directories = $modx->getCollection('directory');
$output .= '<p>Total: '. count($directories) . '</p>';
foreach($directories as $directory) {
$output .= '<p>First Name: ' . $directory->get('firstName');
$output .= '
Last Name: ' . $directory->get('lastName');
$output .= '
membership: ' . $directory->get('membership') . '
</p>';
}
return $output;
//Snippet for resource: [[!CreateXpdoClassesDr? &myPackage=`directories` &myPrefix=`organizer_`]]
page output:
Show Directory
added Package
Total: 0
Error Log:
[2017-06-22 23:38:56] (ERROR @ /home/leelogic/public_html/dev/core/xpdo/xpdo.class.php : 761) directory::loadCollection() is not a valid static method.
[2017-06-22 23:38:56] (ERROR @ /home/leelogic/public_html/dev/core/cache/includes/elements/modsnippet/86.include.cache.php : 16) PHP warning: Invalid argument supplied for foreach()
Here is the page after creating xpdo
[2017-06-23 02:33:48] (WARN @ /home/leelogic/public_html/dev/core/xpdo/om/mysql/xpdogenerator.class.php : 199)
No indexes were found in table organizer_directory
[2017-06-23 02:33:48] (INFO @ /home/leelogic/public_html/dev/core/cache/includes/elements/modsnippet/85.include.cache.php : 207)
Schema file written to /home/leelogic/public_html/dev/core/components/directories/schema/directories.mysql.schema.xml
[2017-06-23 02:33:48] (INFO @ /home/leelogic/public_html/dev/core/cache/includes/elements/modsnippet/85.include.cache.php : 218)
Schema file parsed -- Files written to /home/leelogic/public_html/dev/core/components/directories/model/
[2017-06-23 02:33:48] (INFO @ /home/leelogic/public_html/dev/core/cache/includes/elements/modsnippet/85.include.cache.php : 224)
FINISHED
Did you use the CreateXpdoClasses extra to create the class and map files based on your imported table?
If you wrote the schema yourself, make sure your directory object extends xPDOSimpleObject instead of xPDOObject. That way it will always have an ID field to use as an index.