We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45634
    • 23 Posts
    I have seen the $modx->addPackage method, and understand its use. However where should this be added?

    Because if you look at the assets/connector for Doodle it isn't there and that works. https://github.com/splittingred/Doodles/blob/develop/assets/components/doodles/connector.php

    However, just in case I did try already adding addPackage() to my connector:

    $modx->whisky = new Whisky($modx);
    $modx->lexicon->load('whisky:default');
    $modx->addPackage('whisky', $modx->whisky->config['modelPath']);
    /* handle request */


    Still the same problem. Or are you saying this should be added elsewhere?
      • 4172
      • 5,888 Posts
      this is done here:


      https://github.com/splittingred/Doodles/blob/develop/core/components/doodles/model/doodles/doodles.class.php#L30
      [ed. note: Bruno17 last edited this post 12 years, 10 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 45634
        • 23 Posts
        Quote from: Bruno17 at Nov 08, 2013, 03:31 AM
        this is done here:


        https://github.com/splittingred/Doodles/blob/develop/core/components/doodles/model/doodles/doodles.class.php#L30

        Ah yes. Tho as am I, so still no solution :-(


        <?php
        /**
         * @package whisky
         */
        class Whisky {
            /**
             * Constructs the whisky object
             *
             * @param modX &$modx A reference to the modX object
             * @param array $config An array of configuration options
             */
            function __construct(modX &$modx,array $config = array()) {
                $this->modx =& $modx;
        
                $basePath = $this->modx->getOption('whisky.core_path',$config,$this->modx->getOption('core_path').'components/whisky/');
                $assetsUrl = $this->modx->getOption('whisky.assets_url',$config,$this->modx->getOption('assets_url').'components/whisky/');
                $this->config = array_merge(array(
                    'basePath' => $basePath,
                    'corePath' => $basePath,
                    'modelPath' => $basePath.'model/',
                    'processorsPath' => $basePath.'processors/',
                    'templatesPath' => $basePath.'templates/',
                    'chunksPath' => $basePath.'elements/chunks/',
                    'jsUrl' => $assetsUrl.'js/',
                    'cssUrl' => $assetsUrl.'css/',
                    'assetsUrl' => $assetsUrl,
                    'connectorUrl' => $assetsUrl.'connector.php',
                ),$config);
        
                if (!$this->modx->addPackage('whisky',$this->config['modelPath'])) {
        			die("COULD NOT ADD MODEL");
        		};
            }
          • 45634
          • 23 Posts
          So in an attempt to trace the problem I have produced a really simple code set:

          This was not added as a NameSpace into Modx.

          Create scheme file:

          <?xml version="1.0" encoding="UTF-8"?>
          <model package="fwhisky" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
              <object class="FWhiskyBrand" table="fwhisky_brand" extends="xPDOSimpleObject">
                  <field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              </object>
              <object class="FWhiskyExpression" table="fwhisky_expression" extends="xPDOSimpleObject">
                  <field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              </object>
          </model>


          Run generater:

          <?php
          require_once dirname(__FILE__).'/build.config.php';
          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');
          $basePath = $modx->getOption('fwhisky.core_path',null,$modx->getOption('core_path').'components/fwhisky/');
          $sources = array(
              'model' => $basePath.'model/',
              'schema_file' => $basePath.'model/schema/fwhisky.mysql.schema.xml'
          );
          $manager= $modx->getManager();
          $generator= $manager->getGenerator();
           
          if (!is_dir($sources['model'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Model directory not found!'); die(); }
          if (!file_exists($sources['schema_file'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Schema file not found!'); die(); }
          $generator->parseSchema($sources['schema_file'],$sources['model']);
          $modx->addPackage('fwhisky', $sources['model']); // add package to make all models available
          $manager->createObjectContainer('FWhiskyBrand'); // created the database table
          $manager->createObjectContainer('FWhiskyExpression'); // created the database table
          $modx->log(modX::LOG_LEVEL_INFO, 'Done!');


          This builds the model files:

          <?php
          class FWhiskyBrand extends xPDOSimpleObject {}


          <?php
          class FWhiskyExpression extends xPDOSimpleObject {}


          <?php
          
          $xpdo_meta_map = array (
            'xPDOSimpleObject' => 
            array (
              0 => 'FWhiskyBrand',
              1 => 'FWhiskyExpression',
            ),
          );


          <?php
          require_once (dirname(dirname(__FILE__)) . '/fwhiskybrand.class.php');
          class FWhiskyBrand_mysql extends FWhiskyBrand {}


          <?php
          $xpdo_meta_map['FWhiskyBrand']= array (
            'package' => 'fwhisky',
            'version' => NULL,
            'table' => 'fwhisky_brand',
            'extends' => 'xPDOSimpleObject',
            'fields' => 
            array (
              'title' => '',
            ),
            'fieldMeta' => 
            array (
              'title' => 
              array (
                'dbtype' => 'varchar',
                'precision' => '255',
                'phptype' => 'string',
                'null' => false,
                'default' => '',
              ),
            ),
          );



          <?php
          require_once (dirname(dirname(__FILE__)) . '/fwhiskyexpression.class.php');
          class FWhiskyExpression_mysql extends FWhiskyExpression {}


          <?php
          $xpdo_meta_map['FWhiskyExpression']= array (
            'package' => 'fwhisky',
            'version' => NULL,
            'table' => 'fwhisky_expression',
            'extends' => 'xPDOSimpleObject',
            'fields' => 
            array (
              'title' => '',
            ),
            'fieldMeta' => 
            array (
              'title' => 
              array (
                'dbtype' => 'varchar',
                'precision' => '255',
                'phptype' => 'string',
                'null' => false,
                'default' => '',
              ),
            ),
          );


          core/components/fwhisky/
          core/components/fwhisky/model
          core/components/fwhisky/model/fwhisky
          core/components/fwhisky/model/fwhisky/mysql
          core/components/fwhisky/model/fwhisky/mysql/fwhiskyexpression.class.php
          core/components/fwhisky/model/fwhisky/mysql/fwhiskyexpression.map.inc.php
          core/components/fwhisky/model/fwhisky/mysql/fwhiskybrand.map.inc.php
          core/components/fwhisky/model/fwhisky/mysql/fwhiskybrand.class.php
          core/components/fwhisky/model/fwhisky/fwhiskyexpression.class.php
          core/components/fwhisky/model/fwhisky/metadata.mysql.php
          core/components/fwhisky/model/fwhisky/fwhiskybrand.class.php
          core/components/fwhisky/model/schema
          core/components/fwhisky/model/schema/fwhisky.mysql.schema.xml
          


          Just to be paranoid, I've checked all the above files are world readable!

          Then, lets create a simple test page:

          <?php
          require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
          require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
          require_once MODX_CONNECTORS_PATH.'index.php';
          
          $corePath = $modx->getOption('fwhisky.core_path',null,$modx->getOption('core_path').'components/fwhisky/');
          
          
          
          if (!$modx->addPackage('fwhisky', $corePath."model") ) {
          	print "CANT ADD";
              die("CANT ADD");
          }
          
          
          print "Path: ".$corePath."model"."<p>";
          
          $class = "FWhiskyBrand";
          
          $c = $modx->newQuery($class);
          $brands = $modx->getCollection($class,$c);
          
          
          
          print "End";
          


          When we run it we get

          Path: /home/james/modx/core/components/fwhisky/model
          
          End


          I've checked that path ...

          james@debian:~/modx$ ls -al /home/james/modx/core/components/fwhisky/model
          total 16
          drwxr-xr-x 4 james james 4096 Nov  8 10:04 .
          drwxr-xr-x 3 james james 4096 Nov  8 10:21 ..
          drwxr-xr-x 3 james james 4096 Nov  8 10:04 fwhisky
          drwxr-xr-x 2 james james 4096 Nov  8 09:28 schema


          But now, we check the error log:
           [2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) Could not load class: FWhiskyBrand from mysql.fwhiskybrand.
          [2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) Could not load class: FWhiskyBrand from mysql.fwhiskybrand.
          [2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) FWhiskyBrand::loadCollection() is not a valid static method.


          So basically, I can't seem to make Modx see any models I create. At all. Any ideas would be greatly appreciated! Thanks,
            • 4172
            • 5,888 Posts
            done with MIGXdb in some Minutes:

            In the MIGX-CMP I did

            1. 'Create Package' with name: fwhisky
            2. 'Added your schema'
            3. 'Parse Schema'
            4. 'Create Tables'
            5. Created a MIGX-config with name "fwhisky":

            {
              "formtabs":[
                {
                  "MIGX_id":1,
                  "caption":"fwhisky",
                  "print_before_tabs":"0",
                  "fields":[
                    {
                      "MIGX_id":1,
                      "field":"title",
                      "caption":"Title",
                      "description":"",
                      "description_is_code":"0",
                      "inputTV":"",
                      "inputTVtype":"",
                      "configs":"",
                      "sourceFrom":"config",
                      "sources":"[]",
                      "inputOptionValues":"",
                      "default":""
                    }
                  ]
                }
              ],
              "contextmenus":"update||remove",
              "actionbuttons":"addItem",
              "columnbuttons":"",
              "filters":"[]",
              "extended":{
                "migx_add":"",
                "formcaption":"",
                "update_win_title":"",
                "win_id":"",
                "maxRecords":"",
                "addNewItemAt":"bottom",
                "multiple_formtabs":"",
                "extrahandlers":"",
                "packageName":"fwhisky",
                "classname":"FWhiskyBrand",
                "task":"",
                "getlistsort":"",
                "getlistsortdir":"",
                "use_custom_prefix":"0",
                "prefix":"",
                "grid":"",
                "gridload_mode":1,
                "check_resid":1,
                "check_resid_TV":"",
                "join_alias":"",
                "has_jointable":"yes",
                "getlistwhere":"",
                "joins":"",
                "cmpmaincaption":"fwhisky",
                "cmptabcaption":"FWhiskyBrand",
                "cmptabdescription":"",
                "cmptabcontroller":"",
                "winbuttons":"",
                "onsubmitsuccess":"",
                "submitparams":""
              },
              "columns":[
                {
                  "MIGX_id":1,
                  "header":"id",
                  "dataIndex":"id",
                  "width":"",
                  "sortable":"false",
                  "show_in_grid":1,
                  "renderer":"",
                  "clickaction":"",
                  "selectorconfig":"",
                  "renderchunktpl":"",
                  "renderoptions":"[]"
                },
                {
                  "MIGX_id":2,
                  "header":"title",
                  "dataIndex":"title",
                  "width":"",
                  "sortable":"false",
                  "show_in_grid":1,
                  "renderer":"",
                  "clickaction":"",
                  "selectorconfig":"",
                  "renderchunktpl":"",
                  "renderoptions":"[]"
                }
              ]
            }


            6. called the CMP with &configs=fwhisky

            There was it, the working CMP
            Thats basically all you need to create your CMP

              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 45634
              • 23 Posts
              Quote from: Bruno17 at Nov 08, 2013, 04:59 AM

              Thats basically all you need to create your CMP

              I had a look at your stuff and read thru the instructions: does Migx generate the CMP code and write it to your component? I really need that so I can make small tweaks to it later! There are a lot more fields to add once I get the basics working.

              If you look at my previous post I've actually narrowed it down to a problem not in the CMP code but the xPDO code - if you had any suggestions for that it would be great! Thanks,
                • 45634
                • 23 Posts
                Quote from: jarofgreen at Nov 08, 2013, 04:39 AM

                So basically, I can't seem to make Modx see any models I create. At all. Any ideas would be greatly appreciated! Thanks,

                I just ran thru the steps in post #14 on a completely fresh install on a completely different machine, and I got exactly the same error. There must be something fundamentally and basically wrong I'm doing, as creating model and then referencing them should be a simple task? Any help appreciated,
                Thanks,
                James
                • discuss.answer
                  • 45634
                  • 23 Posts
                  Fixed the problem I was having in issue #14 with the help of a user on stack overflow!
                  http://stackoverflow.com/questions/19863665/cant-create-and-reference-model-in-modx/19869485?noredirect=1#19869485

                  "Modx/xPDO has a massive thing with trailing slashes on paths... have you tried adding a trailing slash to your .../path/to/model/ ?"

                  Change to:

                  if (!$modx->addPackage('fwhisky', $corePath."model/") ) {
                      print "CANT ADD";
                      die("CANT ADD");
                  }
                  


                  works!

                  But I still couldn't get the original whisky code to work. But by comparing carefully fwhisky vs whisky, I tracked it down. See this connecter from post #5 ...

                  $corePath = $modx->getOption('whisky.core_path',null,$modx->getOption('core_path').'components/whisky/');
                  require_once $corePath.'model/whisky/whisky.class.php';
                  require_once $corePath.'model/whisky/brand.class.php';
                  $modx->whisky = new Whisky($modx);


                  Delete require brand.class.php. For some reason, if PHP has already loaded the class file, Modx won't load it and it will think it can't! I don't know if this should be reported as a bug?

                  So I now have a CPM grid with some dummy data showing!

                  (I now have bugs in that to work thru, like why isn't ID showing .... but at least I've managed to progress smiley )

                  Thanks for the help everyone!
                    • 45634
                    • 23 Posts
                    Some minor Tweaks to JS CPM code, and ...

                    Right, I'm off for a drink now. Thanks again.