We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50012
    • 12 Posts
    I have a xPDO/PHP-Class or may a logical problem.

    I have a master Class "Event" that has a function that will add dates to a date table. Very simplified it looks like that:
    class Event extends xPDOSimpleObject {
        public function addDate {
            $date = $this->modx->newObject('eventDate');
            $date->set('title', $title);
            $date->save();
        }
    }


    This works perfect and the addDate function adds the date to the table. Now i would like to move this function inside the eventDate Class. But this does not work anymore. Simplified it looks like that:
    class eventDate extends xPDOSimpleObject {
        public function addDate {
            $this->set('title', $title);
            $this->save();
        }
    }


    I thought i could just add new dates like this. But this does not work. Is there a good example how to communicate via xPDO with the DB inside the Class/Object itself?

    This question has been answered by multiple community members. See the first response.

      • 3749
      • 24,544 Posts
      If $this is an actual, instantiated object before calling the method, it should work. Otherwise, xPDO doesn't have an object to save to the DB. You either need to call newObject() or retrieve the object you want to update from the DB first.



        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
      • I think you oversimplified the code you shared, as that's not valid syntax and will cause a fatal error. I'm also finding it a bit hard to understand the intent here.

        The code you shared, once de-simplified and made valid again, would work like this:

        $eventDate = $modx->newObject('eventDate');
        $eventDate->addDate($title); // assuming $title is supposed to be passed in?


        If you want to skip the newObject bit, you could use a static method to do something like this:

        $eventDate = eventDate::addDate($modx, $title);

        for which the addDate code would look something like:

        (Note eventDate has to be loaded into memory for static methods to work; xPDO doesn't do that automatically and model classes aren't autoloaded. Personally I've added a list of classes with static methods/constants into the service constructor, calling loadClass for each, to make sure they're always available everywhere.)

        public static function addDate(modX $modx, $title) {
           $inst = $modx->newObject('eventDate');
           $inst->set('title', $title);
           $inst->save();
           return $inst;
        }
        
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 50012
          • 12 Posts
          Thanks a lot for your support. I think the problem is, that i don't really understand how to work with multi model classes in MODX.

          What i try to do is to separate all model relatet functions into their model class: Event-Functions inside the event model class, Team-Functions inside the team model class, etc. I probably don't load/include them correctly into the snippet - but i can't find any tutorial/docs on it. The doodle example always just handles a single class.

          So my snippet looks like that:
          $sgf = $modx->getService('signfy','Signfy',$modx->getOption('signfy.core_path',null,$modx->getOption('core_path').'components/signfy/').'model/signfy/',$scriptProperties);
          if (!($sgf instanceof Signfy)) return 'Error';
          
          $signfy = new Signfy($modx, array());
          return $signfy->test('test');


          My signfy.class.php looks like that:
          <?php
          class Signfy {
          
              public $modx;
              public $config = array();
              public function __construct(modX &$modx,array $config = array()) {
                  $this->modx =& $modx;
                  $basePath = $this->modx->getOption('signfy.core_path',$config,$this->modx->getOption('core_path').'components/signfy/');
                  $assetsUrl = $this->modx->getOption('signfy.assets_url',$config,$this->modx->getOption('assets_url').'components/signfy/');
                  $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);
          
                  $this->modx->addPackage('signfy',$this->config['modelPath']);
              }
          
          
              public function test($title) {
                  return $title;
              }
          
          }
          


          And now i would like to call a function of the sgfevent.class.php
          <?php
          class sgfEvent extends xPDOSimpleObject {
          
          
              public function addEvent($title) {
                  return 'Works';
              }
          
          
          }


          I think separating all function into their model classes would be the correct pattern. But now i am pretty stuck here. Working in a single Class works good. But i don't get them separated.
          • discuss.answer
            And now i would like to call a function of the sgfevent.class.php

            To do that, first you create an instance of the sgfEvent class. As it's an xPDO(Simple)Object, you'd typically either create a new object, or get an object for that. In your snippet, after you loaded your Signfy service class that calls $this->modx->addPackage, you'd do something like this:

            $event = $modx->getObject('sgfEvent', ['id' => 10]);
            $event->addEvent('foo bar');
            


            Or, for a new object:
            $event = $modx->newObject('sgfEvent');
            $event->set('some_field', 'some value');
            $event->addEvent('foo bar');
            


              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • discuss.answer
              • 3749
              • 24,544 Posts
              You might find this useful.
                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
                • 50012
                • 12 Posts
                Thank you very much. Both helped me a lot to get a better understanding of xPDO/Objects and Classes. Thanx!