We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35227
    • 5 Posts
    I created a schema:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <model package="boxexpert"
           baseClass="xPDOObject"
           platform="mysql"
           defaultEngine="MyISAM"
           phpdoc-package="boxexpert"
           phpdoc-subpackage="model">
    
        <object class="beCategory" table="category" extends="xPDOObject">
            <field key="id" dbtype="int" precision="11" phptype="integer" null="false" index="pk" generated="native" />
            <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" />
            <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                <column key="id" collation="A" null="false" />
            </index>
            <aggregate alias="All" class="beBox" local="id" foreign="category_id" cardinality="many" owner="local" />
        </object>
    
        <object class="beBox" table="box" extends="xPDOObject">
            <field key="id" dbtype="int" precision="11" phptype="integer" null="false" index="pk" generated="native" />
            <field key="title" dbtype="varchar" precision="255" phptype="string" null="true" />
            <field key="weight" dbtype="int" precision="2" phptype="integer" null="false" />
            <field key="category_id" dbtype="int" precision="11" phptype="integer" null="false" default="1" />
            <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                <column key="id" collation="A" null="false" />
            </index>
            <aggregate alias="Category" class="beCategory" local="category_id" foreign="id" cardinality="one" owner="foreign" />
        </object>
    
    </model>


    And then I created a script that builds classes from it (using the one from the documentation as the template):

    <?php
    /**
     * Build Schema script
     *
     * @package storefinder
     * @subpackage build
     */
    $mtime = microtime();
    $mtime = explode(" ", $mtime);
    $mtime = $mtime[1] + $mtime[0];
    $tstart = $mtime;
    set_time_limit(0);
     
    define('MODX_BASE_PATH', '/www/test/');
    define('MODX_CORE_PATH', MODX_BASE_PATH . 'core/');
    define('MODX_MANAGER_PATH', MODX_BASE_PATH . 'manager/');
    define('MODX_CONNECTORS_PATH', MODX_BASE_PATH . 'connectors/');
    define('MODX_ASSETS_PATH', MODX_BASE_PATH . 'assets/');
    
    include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx= new modX();
    $modx->initialize('mgr');
    $modx->loadClass('transport.modPackageBuilder','',false, true);
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
     
    $root = MODX_BASE_PATH;
    $sources = array(
        'root' => $root,
        'core' => $root.'core/components/boxexpert/',
        'model' => $root.'core/components/boxexpert/model/',
        'assets' => $root.'assets/components/boxexpert/',
        'schema' => $root.'schema/boxexpert/',
    );
    
    $manager= $modx->getManager();
    $generator= $manager->getGenerator();
    
    $generator->parseSchema($sources['schema'].'schema.xml', $sources['model']);
     
    $mtime= microtime();
    $mtime= explode(" ", $mtime);
    $mtime= $mtime[1] + $mtime[0];
    $tend= $mtime;
    $totalTime= ($tend - $tstart);
    $totalTime= sprintf("%2.4f s", $totalTime);
     
    echo "\nExecution time: {$totalTime}\n";
     
    exit ();
    
    ?>


    After I ran it, the following files were created:
    root@Karachi-Desktop:/www/test/core/components# ls
    boxexpert
    root@Karachi-Desktop:/www/test/core/components# ls -R
    .:
    boxexpert
    
    ./boxexpert:
    model
    
    ./boxexpert/model:
    boxexpert
    
    ./boxexpert/model/boxexpert:
    bebox.class.php  becategory.class.php  mysql
    
    ./boxexpert/model/boxexpert/mysql:
    bebox.class.php    becategory.class.php
    bebox.map.inc.php  becategory.map.inc.php


    Then I manually created the required tables in my MySQL modx database.

    And then I tried to use a snippet to access it:
    <?php
    
    /**
     * @package boxexpert
     */
    
    $o = '';
    
    $modx->addPackage('boxexpert', '/www/test/core/components/boxexpert/model/');
    
    $box = $modx->getObject('beBox', 1);
    $o = $o . $box->get('title');
    
    return $o;


    And I got this:
    Fatal error: Call to a member function get() on a non-object in /www/test/core/cache/includes/elements/modsnippet/1.include.cache.php on line 16

    What did I do wrong? How do I fix this? Why does this have to be so hard? huh :’( Help!
      • 28215
      • 4,149 Posts
      Does the primary key in your table have an ID of 1? Doesn’t seem like it.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 35227
        • 5 Posts
        There is a box with id 1, if that’s what you mean.

        UPD: I forgot the modx_ prefix, now everything works. Thank you, I’m so happy right now laugh