We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40944
    • 78 Posts
    Quote from: BobRay at Jul 30, 2013, 05:25 PM
    As I said, I would try renaming your pageID field to 'id' in PhpMyAdmin, making it autoincrement, and specifying it as an index and primary key, then run the script again (after deleting the schema) and see what the schema looks like.
    I am sorry, I wanted to explain this in my previous post.
    I have got contests where users can register. Managers can create new contests by creating a new resource in a specific container and with a special template. In the template I have a formit call and a formit2db hook...
    So to keep track of who is participating in which contest I am using the resource id (pageid) to link contestants to the appropriate contest. Therefor pageid is not unique (more contestants in one contest).

    Thank you for your reply, I am going to wrestle with it a bit more.
      • 3749
      • 24,544 Posts
      That makes sense and my suggestion no longer does. wink

      Let me try again. You need a unique 'id' field in your table that should be the primary key and set to autoincrement, and you need a <field key="id" line in your schema if you're extending xPDOObject, but not if you're extending xPDOSimpleObject. Maybe your schema was created before the 'id' field existed?

      Also, you do know that you need to re-run the script any time the schema changes, right? That's necessary to update the class and map files.

      I'd also suggest this change in your code:


      $success = $this->modx->addPackage('inschrijvingen',$this->config['modelPath'], 'evd_');
      
      if (! $success) {
          $this->modx->log(MODX::LOG_LEVEL_ERROR, 'Error: addPackage() failed');
      }
      
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 40944
        • 78 Posts
        Quote from: BobRay at Jul 30, 2013, 06:02 PM
        That makes sense and my suggestion no longer does. wink

        Let me try again. You need a unique 'id' field in your table that should be the primary key and set to autoincrement, and you need a <field key="id" line in your schema if you're extending xPDOObject, but not if you're extending xPDOSimpleObject. Maybe your schema was created before the 'id' field existed?
        The 'id' field existed all along, but I think because I used the generated classes first it just worked (extending xPDOSimpleObject, like you said). When I tried using the tutorials approach this probably didn't work anymore because of conflicts in the class when extending (not that good with php)?

        Before the tutorial it did work (as well on the front-end as in the manager), but I wanted to be able to manage/edit registrations. So I tried using the tutorial to create controllers, a connector and processors. Before I had only a connector which looked like:
        /**
         * Controller for Ajax requests.
         */
        // Adjust the path appropriately
        require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
        if (!defined('MODX_API_MODE')) {
            define('MODX_API_MODE', false);
        }
        include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
         
        $modx = new modX();
        $modx->initialize('mgr');
         
        if (!$modx->hasPermission('view_document')) {
            header('HTTP/1.0 401 Unauthorized');
            print 'Operation not allowed.';
            exit;
        }
        // These are the standard values that are posted to the Ajax URL by Ext JS
        $start = (int) $modx->getOption('start',$_POST,0);
        $limit = (int) $modx->getOption('limit',$_POST,20);
        $sort = $modx->getOption('sort',$_POST);
        $dir = $modx->getOption('dir',$_POST,'ASC');
         
        // error_log(print_r($_POST,true));  // <--- uncomment this for debugging
        
        $path = MODX_CORE_PATH . 'components/inschrijvingen/';
        $result = $modx->addPackage('inschrijvingen',$path . 'model/','evd_');
        
        $c = $modx->newQuery('Inschrijvingen');
        $count = $modx->getCount('Inschrijvingen',$c);
        
        $pages = $modx->getCollection('Inschrijvingen');
        
        $c->sortby($sort,$dir);
        $c->limit($limit,$start);
         
        //$c->prepare(); error_log($c->toSQL()); // <-- uncomment for debugging
         
        //$pages = $modx->getCollection('modResource',$c);
        
         
        $list = array();
        foreach ($pages as $p) {
            $array = $p->toArray();
            $list[] = $array; 
        }
         
        // The format of the output is not well documented, but it requires a node for "total" and "rows"
        print '{"total":"'.$count.'","results":'.$modx->toJSON($list).',"success":true,"msg":"Got our rows..."}';
         
        //error_log(print_r($list,true));  // <-- another debugging point
         
        @session_write_close();
        exit();
        

        You will probably recognize this, because it is from your tutorial.
        Also, you do know that you need to re-run the script any time the schema changes, right? That's necessary to update the class and map files.
        Yup, did it a few times already (forgot some fields, made some mandatory etc). It's a fantastic snippet!
          • 3749
          • 24,544 Posts
          You probably know this, but you don't want to be calling session_write_close() and exit() in a connector.

          And I think you'll be happier extending xPDOSimpleObject.

          Be sure to add the error log line I suggested above to make sure addPackage() was successful. If addPackage() is failing, it would explain a lot of your problems.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • discuss.answer
            • 40944
            • 78 Posts
            Quote from: BobRay at Jul 30, 2013, 06:36 PM
            You probably know this, but you don't want to be calling session_write_close() and exit() in a connector.
            I figured smiley but that was the old connector, before I started trying to use processors.

            And I think you'll be happier extending xPDOSimpleObject.
            How would I do that? when I let my Inschrijvingen class extend xPDOSimpleObject it only returns blank pages (unless I comment (remove) the constructor).

            Be sure to add the error log line I suggested above to make sure addPackage() was successful. If addPackage() is failing, it would explain a lot of your problems.
            The addPackage() function is working smiley it definitely has to do with the connector/Inschrijvingen.class.php file.

            I checked out quips code again and noticed some differences. I realize now I didn't really name my extra/table in a good way. I still have no idea how to solve this though. I guess it's best to start over and pay more attention to naming my extra and table differently.
              • 3749
              • 24,544 Posts
              CMPs are difficult to deal with at first. I sometimes just create a form in the front end that bails out if you're not logged into the Manager:


              if (! $modx->user->hasSessionContext('mgr')) {
                  die ('Unauthorized');
              }


              FWIW, I never try to create a CMP until after I get all the basic code working first, usually in a .PHP file I run outside of MODX (in my code editor, PhpStorm, where I can run it in the debugger). You can instantiate MODX in a few lines, set any $scriptProperties (if you need them), and it will run just like a snippet.

              That way I can get all the bugs out of the schema, class, and CRUD code before wrestling with the CMP part of it.

              If you get it running as a snippet, you can also just make a CMP out of it by creating an action index.php file that calls $modx->runSnippet() (see the new version of the RefreshCache extra). It won't have all the bells and whistles (or the security) you get with controllers, connectors, processors, etc., but it's *way* simpler.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 40944
                • 78 Posts
                I created a new MODX install and started developing using the tutorial and it is starting to work now. I took the Javascript files and the schema from the old one though. At least the connector works, so I think it will all work out now.

                Thanks for everything, when new questions arise I'll post here or in a new topic smiley

                Hoping to publish this as a snippet one day.
                  • 40944
                  • 78 Posts
                  Okay, I made a working CMP now smiley really happy it finally worked out:)

                  But I have a followup question. I would like to publish an extra for managing contests and contestants. But no contest is the same, for example a self defense club would ask for name, age, weight and belt-color (or something). So I was wondering: would it be possible to create an extra which you setup with some fields on install.

                  I guess this would mean generating part of the javascript, schema, snippets and probably something I am forgetting.

                  I know it is possible to launch a setup window on install, I have seen an extra do that (quip?).

                  Thanks in advance,
                  Erwin
                    • 4172
                    • 5,888 Posts
                    you could put the extra-fields into one text-field as a json-string.
                    you may have a configurator in your CMP, which lets the user add/remove extended fields.
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 3749
                      • 24,544 Posts
                      Yes, you can have an install script with a form and a resolver that uses the data from the form to do whatever you want during the install. The form will be shown to the user as the first step of the install process.

                      There's a partial example here: https://github.com/BobRay/MyComponent/blob/master/core/components/mycomponent/elements/chunks/user.input.php

                      The form: https://github.com/BobRay/SPForm/blob/master/_build/user.input.php

                      In the "Install" section of the Resolver, the form fields will be available in the $options array:

                      $newSiteName = $options['sitename']


                      In the build.transport.php file, this goes in the array with the readme, license, and changelog:

                      'setup-options' => array(
                              'source' => $sources['install_options'] . 'user.input.php',
                          );


                      If you use MyComponent, you just need to set this option in the project config file and a skeleton user.input.php and resolver will be created for you, and build.transport.php will handle inserting them automatically:

                      'install.options' => 'install.options',
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting