Initialize Schema From Within a Parent Class

Body
A simple mechanism which can be used to initialize the database schema from within the main package class file.

Authored on

Framework

Constructor Considerations

<?php
/**
 * Main constructor for the MyClass Class
 *
 * @param modX $modx
 *            A reference to the current modx Object
 * @param array $config
 *            configuration settings which can be used to over ride default settings - typically unnecessary
 */
function __construct(modX &$modx, array $config = array())
{
    try {

        /**
         * The MODX object.
         */
        $this->modx = &$modx;

        /**
         * The current MODX Revolution User
         */
        $this->user = &$this->modx->user;

        /**
         * Establish system paths which will be used by this class.
         */
        $corePath = $this->modx->getOption(__CLASS__ . '.core_path', $config, $this->modx->getOption('core_path') . 'components/' . __CLASS__ . '/');
        $assetsUrl = $this->modx->getOption(__CLASS__ . '.assets_url', $config, $this->modx->getOption('assets_url') . 'components/' . __CLASS__ . '/');
        $this->config = array_merge(array(
            'assetsUrl' => $assetsUrl,
            'cssUrl' => $assetsUrl . 'css/',
            'jsUrl' => $assetsUrl . 'js/',
            'imagesUrl' => $assetsUrl . 'images/',
            'connectorUrl' => $assetsUrl . 'connector.php',

            'corePath' => $corePath,
            'modelPath' => $corePath . 'model/',
            'chunksPath' => $corePath . 'elements/chunks/',
            'controllersPath' => $corePath . 'controllers/',
            'processorsPath' => $corePath . 'processors/',
            'snippetsPath' => $corePath . 'elements/snippets/',

            'package' => __CLASS__,
            'prefix' => 'nsit_'
        ), $config);

        $this->modx->addPackage($this->config['package'], $this->config['modelPath'], $this->config['prefix']);
    } catch (xPDOException $xe) {
        $this->modx->sendError('unavailable', array(
            'error_message' => $xe->getMessage()
        ));
    } catch (Exception $e) {
        $this->modx->sendError('unavailable', array(
            'error_message' => $e->getMessage()
        ));
    }
}

Initialize Function

<?php 
/**
 * Parses the schema and generates ORM files and the database structure, but will not overwrite preexisting files such as IonCube Encoded Versions or those previously generated.
 *
 * Schema file must be stored at /core/components/[package_name]/model/schema/[package_name].mysql.schema.xml.
 * Schema file must be named [package_name].mysql.schema.xml.
 *
 * @return string
 */
public function initializeSchema()
{
    $out = 'failed';
    $xpdoManager = $this->modx->getManager();
    if ($xpdoManager) {
        $xpdoGenerator = $xpdoManager->getGenerator();
        if ($xpdoGenerator) {
            $success = $xpdoGenerator->parseSchema($this->config['modelPath'] . 'schema/' . $this->config['package'] . '.mysql.schema.xml', $this->config['modelPath']);
            if ($success) {
                $schemaObjects = $this->getSchemaObjectNames();
                foreach ($schemaObjects as $t) {
                    $success += $xpdoManager->createObjectContainer(trim($t));
                }
                $out = ($success > count($schemaObjects)) ? 'Model Created at ' . $this->config['modelPath'] . $this->config['package'] . ' and all tables successfully created.

 

' : 'Operation Failed'; } } } $this->logevent(($success > count($schemaObjects)) ? true : false, $out); return $out; }

Package Class Names

<?php
/**
 * Retrieves an array of the object names defined in the schema.
 * Note: this list must be manually updated and match schema definitions for
 * all subsequent usage to function properly.
 *
 * @return array An collection of object names established in the schema file.
 */
private function getSchemaOjects() {
    return array (
            'spCast',
            'spCastHistory',
            'spCastInformation',
            'spCastPayrol',
            'spCastNeeds',
            'spEvent',
            'spEventCast',
            'spEventSpeaker',
            'spLog', 
    );
}