Hi
I’m trying to understand how to work with external tables in my snippets. I have created a schema and generated a package from that. I also have the mysql tables in place and what I can see, everything works so far. Now, i’m trying to create a snippet that will act as a hook for FormIt and save form data into my db tables. I will also do some further processing with the data and wan’t to have a base class with some helper functions.
Here is my snippet code so far:
<?php
/**
* @package entry
*/
require_once $modx->getOption('entry.core_path',null,$modx->getOption('core_path').'components/entry/').'model/entry/entry.class.php';
$ent = new Entry($modx,$scriptProperties);
if (!($ent instanceof Entry)) {
$modx->log(modX::LOG_LEVEL_ERROR,'[Entry] Could not load Entry class.');
return false;
}
$competitorid =& $modx->user->id;
$entrant = $modx->newObject('eResults');
$fields = $hook->getValues();
$tshirtsize = $fields['resultsThirtSize'][0];
$entrant->fromArray(array(
'competitorid' => $competitorid,
'tshirtsize' => $tshirtsize,
'lastmodified' => $_SERVER['REQUEST_TIME'],
'timeregistered' => $_SERVER['REQUEST_TIME'],
));
$entrant->save();
return true;
?>
And here is my base class:
<?php
/**
* Entry
*
* Copyright 2011 by Kristoffer Karlström <[email protected]>
*
* @package entry
*/
class Entry {
/**
* Creates an instance of the Entry class.
*
* @param modX &$modx A reference to the modX instance.
* @param array $config An array of configuration parameters.
* @return Entry
*/
function __construct(modX &$modx,array $config = array()) {
$this->modx =& $modx;
/* allows you to set paths in different environments
* this allows for easier SVN management of files
*/
$corePath = $this->modx->getOption('entry.core_path',null,MODX_CORE_PATH.'components/entry/');
/* loads some default paths for easier management */
$this->config = array_merge(array(
'corePath' => $corePath,
'modelPath' => $corePath.'model/',
),$config);
$this->modx->addPackage('entry',$this->config['modelPath']);
}
}
?>
When i’m trying to run the snippet, I get an error stating Call to a member function fromArray() on a non-object in ... pointing to this line in my snippet: $entrant = $modx->newObject(’eResults’);
I’m also getting this input in the modx error log: (ERROR @ /index.php) Could not load class: eResults from mysql.eresults.
I won’t get my own error message in the error log som my Entry class should be loaded, and the package should then also be loaded, right? But I can’t use any xpdo functions on my package.
Now to my big confusion. If i’m changing my snippet code to not load the base class, everything works. Here is my working snippet:
<?php
/**
* @package entry
*/
$modx->addPackage('entry',$modx->getOption('core_path').'components/entry/model/');
$competitorid =& $modx->user->id;
$entrant = $modx->newObject('eResults');
$fields = $hook->getValues();
$tshirtsize = $fields['resultsThirtSize'][0];
$entrant->fromArray(array(
'competitorid' => $competitorid,
'tshirtsize' => $tshirtsize,
'lastmodified' => $_SERVER['REQUEST_TIME'],
'timeregistered' => $_SERVER['REQUEST_TIME'],
));
$entrant->save();
return true;
?>
What am I doing wrong?
I’m attaching my schema file and a screenshot of my package folder structure.
// Kristoffer