$dood = $modx->getService('doodles','Doodles',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/doodles/').'model/doodles/',$scriptProperties);
if (!($dood instanceof Doodles)) return '';
/* setup default properties */
$tpl = $modx->getOption('tpl',$scriptProperties,'rowTpl');
$sort = $modx->getOption('sort',$scriptProperties,'name');
$dir = $modx->getOption('dir',$scriptProperties,'ASC');
$modx->setOption('xPDO::OPT_AUTO_CREATE_TABLES',true);
$m = $modx->getManager();
$m->createObjectContainer('Doodle');
return 'Table created.';
<?php $modx->setOption(xPDO::OPT_AUTO_CREATE_TABLES, true);
<?php
$invoice= $modx->getService('invoice','Invoice',MODX_CORE_PATH.'packages/mscd/model/mscd/',$scriptProperties);
if (!($invoice instanceof Invoice))
{return 'fail';}
$m = $modx->getManager();
$results = $m->createObjectContainer('Invoice');
$msg = ($results) ? 'Invoice Table created.' : 'Invoice Table failed.';
return $msg;Fatal error: Class 'Invoice_mysql' not found in /home/my/path/core/xpdo/xpdo.class.php on line 550
First, you definitely don’t want to point to the core/packages/ directory for your model, i.e. MODX_CORE_PATH.’packages/mscd/model/mscd/’ is not likely what you want.
Second of all, why are you using getService to try and load a table class? This is for service classes, which are not generated classes in your model; these are typically a main class which provides business logic and/or common methods for your custom model. All you need to do to be able to create your table is to call $modx->addPackage() and you should be good to go.
<?php
$modx->addPackage('mscd',MODX_CORE_PATH.'components/mscd/model/mscd/','');
$m = $modx->getManager();
$results = $m->createObjectContainer('Invoice');
$msg = ($results) ? 'Invoice Table created.' : 'Invoice Table failed.';
return $msg;(ERROR @ /index.php) Could not load class: Invoice from mysql.invoice.
No, it should be pointing to the root location of your model folder, not to the actual folder with the package name, i.e.
I have changed it to MODX_CORE_PATH.’components/mscd/model/mscd/’. Is that better/correct?
MODX_CORE_PATH.'components/mscd/model/';
No, the Doodles tutorial is describing how to create an Extra that is loaded via a service class. This class serves as a central class to the custom Doodles model, but is not representing a custom table itself. It is responsible for adding the package and other things that some Extras might want to do. Getting the xPDOManager instance and calling it’s method to create the table could be done with or without this service class. In your case, you don’t have a service class, so you can simply add the package and create the table.
Related question I forgot to ask before: the return statement in the code block from the 1st post came from the Doodles tutorial, and I think is poorly written (see the author’s result). I think it should be replaced in the tutorial with one like I have above, based on the actual results of createObjectContainer. Do you agree? If so, I can update the tutorial accordingly.
$m = $modx->getManager();
$m->createObjectContainer('Doodle');
return 'Table created.';$m = $modx->getManager();
return ($m->createObjectContainer('Doodle')) ? 'Table created.' : 'Table not created';