We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27519
    • 275 Posts
    I use a transient class in my module. This class has a class file but no counter part map file as it is not stored in the database. It’s primary use is to handle collections of other -persistent- classes.

    When I want to use this class in a snippet, MODx throws an error that the class cannot be found although the correct package has been added.

    What do I need to do to use this class in a snippet?
      MODx Revolution / MAMP / OS X
      • 28215
      • 4,149 Posts
      Loads class MyClass in /www/extra/myclass.class.php:
      $modx->loadClass('MyClass','/www/extra/',true,true);
      $m = new MyClass();
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 27519
        • 275 Posts
        Thanks. I tried it but I keep getting an error that the class can’t be found.

        The code:
          $modx->loadClass('Calendar', '/usr/home/mydomain/public_html/core/components/tariffs/model/', true, true);
          $calendar = new Calendar($property, $modx);
        


        The log entry:
        [2011-02-03 13:38:28] (ERROR @ /index.php) Could not load class: Calendar from calendar.
        


        I get the same error message when I try to instantiate a new persistent object with (in this snippet "Booking" is a persistent object and mapped to the DB):
          $modx->newObject("Booking");
        


        I’ll probably do something wrong but can’t figure out what...
          MODx Revolution / MAMP / OS X
          • 28215
          • 4,149 Posts
          What’s the file structure look like for your model directory?
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 27519
            • 275 Posts
            See the attached screenshot...

            I copied it from your examples in Doodles and ModExtra.
              MODx Revolution / MAMP / OS X
              • 28215
              • 4,149 Posts
              Try:

              $modx->loadClass('Calendar', '/usr/home/mydomain/public_html/core/components/tariffs/model/tariffs/', true, true);
              $calendar = new Calendar($property, $modx);
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 27519
                • 275 Posts
                Nope, still an error.

                Don’t know whether it’s relevant but the specific snippet is a custom hook called by Formit.

                I use that hook to write form entries to the database.

                I checked out other classes -persistent classes- and none want to load in that specific snippet.
                  MODx Revolution / MAMP / OS X
                  • 28215
                  • 4,149 Posts
                  Post your whole hook and we can debug further.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 27519
                    • 275 Posts
                    Here it is:

                      $modx->addPackage('tariffs', '/usr/home/mydomain/public_html/core/components/tariffs/model/tariffs');
                      $propertyId = $hook->getValue('property-id');  
                      $modx->loadClass('calendar', '/usr/home/mydomain/public_html/core/components/tariffs/model/tariffs', true, true);
                      $property = $modx->getObject('Cottage', $propertyId);
                       // add booking
                      $modx->loadClass('Booking', '/usr/home/mydomain/public_html/core/components/tariffs/model/tariffs', true, false);
                      $booking = $modx->newObject('Booking');
                      $booking->set('title', $hook->getValue('title'));
                      $booking->set('name', $hook->getValue('lastname'));
                      $booking->set('firstname', $hook->getValue('firstname'));
                      $booking->set('email', $hook->getValue('email'));
                      $booking->set('phone', $hook->getValue('phone'));
                      $booking->set('country', $hook->getValue('country'));
                      $booking->set('cottage', $propertyId);
                      $booking->set('arrival_date', $hook->getValue('arrival'));
                      $booking->set('departure_date', $hook->getValue('departure'));
                      $booking->set('tariff', $hook->getValue('tariff'));
                      $booking->set('adults', $hook->getValue('adults'));
                      $booking->set('children', $hook->getValue('children'));
                      $booking->set('dogs', $hook->getValue('dogs'));
                      $booking->set('guest_comments', $hook->getValue('comments'));
                      $booking->calculateDeposit();
                      $booking->set('deposit_paid', 'N');
                      $booking->set('balance_paid', 'N');
                      $booking->set('confirmed', 'N');
                      $booking->set('booking_date', date('Y-m-d', strtotime('now')));
                      $booking->save();
                      // update calendar
                      $calendar = new Calendar($property, $modx);
                      $calendar->markOccupied($booking->get('arrival_date'), $booking->get('departure_date'));
                    
                      return true;
                    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


                    For the loadClass first parameter I wasn’t sure whether to use the filename or the class name. I tried both but it doesn’t seem to make a difference.
                      MODx Revolution / MAMP / OS X
                      • 28215
                      • 4,149 Posts
                      1. You don’t need to loadClass on a class that has maps/classes generated after you run addPackage (such as Booking). Only loadClass on non-generated classes.
                      2. Try this *exactly* in your calendar loadClass:

                      $modx->loadClass('Calendar', '/usr/home/mydomain/public_html/core/components/tariffs/model/tariffs/', true, true);

                        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com