We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37335
    • 13 Posts
    I'm in need of some help as I'm new to all this and still getting my feet wet so to speak. I'm building a custom addon for a client of mine where users can fill in a form to start subscribing to their magazine using the Register snippet. This then creates a modUser for them in the usergroup Subscribers. That part is working fine.

    Then I have a couple extra fields that need saving as well. I've decided to store this data in separate tables to keep things organised. The modx_subscribers table only stores the subscriber's customer number (which is an auto increment field) and the modUser id for linking. The modx_subscriptions table stores the customer number as the link back and the subscription type, date and length (6 or 12 months).

    The XML-schema for these tables looks like this:
    <object class="Subscriber" table="subscribers" extends="xPDOObject">
    	<field key="customernbr" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" generated="native"/>
    	<field key="user" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0"/>
    	<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
    		<column key="customernbr" collation="A" null="false"/>
    	</index>
    
    	<aggregate alias="User" class="modUser" local="user" foreign="id" cardinality="one" owner="foreign"/>
    	<composite alias="Subscription" class="Subscription" local="customernbr" foreign="customernbr" cardinality="many" owner="local"/>
    </object>
    
    <object class="Subscription" table="subscriptions" extends="xPDOSimpleObject">
    	<field key="customernbr" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0"/>
    	<field key="date" dbtype="datetime" phptype="datetime" null="true"/>
    	<field key="type" dbtype="varchar" precision="60" phptype="string" null="false" default=""/>
    	<field key="time" dbtype="varchar" precision="60" phptype="string" null="false" default=""/>
    
    	<aggregate alias="Subscriber" class="Subscriber" local="customernbr" foreign="customernbr" cardinality="one" owner="foreign"/>
    </object>


    The classes are then built using the script from the tutorials in the wiki.

    In the postHook snippet I then have the following code to create the Subscriber object:
    <?php
    $magazinemgmt = $modx->getService('magazinemgmt','MagazineManagement',$modx->getOption('magazinemgmt.core_path',null,$modx->getOption('core_path').'components/magazinemanagement/').'model/magazinemanagement/',$scriptProperties);
    if (!($magazinemgmt instanceof MagazineManagement)) return '';
    
    $user = $hook->getValue('register.user');
    
    $subscriber = $magazinemgmt->modx->newObject('Subscriber');
    $subscriber->set('user', $user->get('id'));
    $subscriber->save();


    From my understanding this should then add the subscriber to the custom table when saved, and a customer number will be auto_incremented and "linked" to the modUser. The problem I'm having though is that modx says it can't load the class.

    [2012-05-29 14:19:21] (ERROR @ /index.php)

    Could not load class: Subscriber from mysql.subscriber.

    What am I missing? The class files have been created and do exist in the model/magazinemanagement/ and model/magazinemanagement/mysql/, just like in the tutorials.

    I also tried to add the package to $modx, but that should have the same effect as the package is loaded in the service call as well the the MagazineManagement object in constructed.

    I could really use your help on this guys. Let me know if you need more info.
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      You get that error when the package isn't added, or the name you specify doesn't match the classname.

      Can you show your MagazineManagement class?
        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.
        • 37335
        • 13 Posts
        This is the MagazineManagement class:
        class MagazineManagement {
        	public $modx = null;
        	public $config = array();
        
        	function __construct(modX &$modx,array $config = array()) {
        		$this->modx =& $modx;
        
        		$corePath = $this->modx->getOption('magazinemgmt.core_path',$config,$this->modx->getOption('core_path').'components/magazinemanagement/');
        		$assetsPath = $this->modx->getOption('magazinemgmt.assets_path',$config,$this->modx->getOption('assets_path').'components/magazinemanagement/');
        		$assetsUrl = $this->modx->getOption('magazinemgmt.assets_url',$config,$this->modx->getOption('assets_url').'components/magazinemanagement/');
        
        		$this->config = array_merge(array(
        			'corePath' => $corePath,
        			'modelPath' => $corePath.'model/',
        			'processorsPath' => $corePath.'processors/',
        			'controllersPath' => $corePath.'controllers/',
                    'templatesPath' => $corePath.'templates/',
        			'chunksPath' => $corePath.'elements/chunks/',
        			'snippetsPath' => $corePath.'elements/snippets/',
        
        			'assetsUrl' => $assetsUrl,
        			'cssUrl' => $assetsUrl.'css/',
        			'jsUrl' => $assetsUrl.'js/',
        			'connectorUrl' => $assetsUrl.'connector.php',
        		),$config);
        
        		$this->modx->addPackage('magazinemanagement',$this->config['modelPath']);
        		
        		$this->initDebug();
        	}
        }

        It's possible something is outdated as I've pretty much built everything by looking at other addons.

        The namespace is magazinemanagement and all settings are prefixed "magazinemgmt." to shorten them a little.
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Hmm nothing obvious stands out..
            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.
            • 37335
            • 13 Posts
            Could there be any underlying issues when the file structure is located in the magazinemanagement folder in the site root? As set up in the Doodles extra tutorial.

            Works fine elsewhere (the CMP), just not in the hook :S

            Also Subscriber extends xPDOObject. There aren't any extra steps needed compared to xPDOSimpleObject I don't think?
              • 33976 ☆ A M B ☆
              • 571 Posts
              Just a quick shout,

              In a hook, don't add the $scriptProperties since it seems to override your class path (add the problem once with FormIt.

              So
              $magazinemgmt = $modx->getService('magazinemgmt','MagazineManagement',$modx->getOption('magazinemgmt.core_path',null,$modx->getOption('core_path').'components/magazinemanagement/').'model/magazinemanagement/');
              


              might do the trick.

              Hope that will help
                • 8386 ☆ A M B ☆
                • 160 Posts
                In your hook is $modx available? also try and check if the MagazineManagement class gets loaded and check if the Subscriber class exists in your hook by doing
                var_dump(class_exists('Subscriber')); die;

                If $modx is available, why are you doing
                $subscriber = $magazinemgmt->modx->newObject('Subscriber');

                instead of
                $subscriber = $modx->newObject('Subscriber');

                and what Romain said is correct [ed. note: easylancer last edited this post 14 years, 4 months ago.]
                  • 37335
                  • 13 Posts
                  Alright, I removed the scriptProperties like Romain said. It made sense and I remember wondering if it was needed or not, but left it there since I didn't think it would make much difference.

                  Also, the var_dump says the class doesn't exist, so apparently it's not loading for some reason... Hmm..

                  Regarding the extra step to $modx, that's just something I tried since it wouldn't work. That's clearly not the cause though.

                  Here are the contents of the class files incase you might notice something is missing (located in model/magazinemanagement/ and generated by the build script, same as doodles)

                  subscriber.class.php
                  <?php
                  class Subscriber extends xPDOObject {}

                  mysql/subscriber.class.php
                  <?php
                  require_once (dirname(dirname(__FILE__)) . '/subscriber.class.php');
                  class Subscriber_mysql extends Subscriber {}

                  mysql/subscriber.map.inc.php
                  <?php
                  $xpdo_meta_map['Subscriber']= array (
                    'package' => 'magazinemanagement',
                    'version' => NULL,
                    'table' => 'subscribers',
                    'extends' => 'xPDOObject',
                    'fields' => 
                    array (
                      'customernbr' => NULL,
                      'user' => 0,
                    ),
                    'fieldMeta' => 
                    array (
                      'customernbr' => 
                      array (
                        'dbtype' => 'int',
                        'precision' => '10',
                        'attributes' => 'unsigned',
                        'phptype' => 'integer',
                        'null' => false,
                        'index' => 'pk',
                        'generated' => 'native',
                      ),
                      'user' => 
                      array (
                        'dbtype' => 'int',
                        'precision' => '10',
                        'attributes' => 'unsigned',
                        'phptype' => 'integer',
                        'null' => false,
                        'default' => 0,
                      ),
                    ),
                    'indexes' => 
                    array (
                      'PRIMARY' => 
                      array (
                        'alias' => 'PRIMARY',
                        'primary' => true,
                        'unique' => true,
                        'columns' => 
                        array (
                          'customernbr' => 
                          array (
                            'collation' => 'A',
                            'null' => false,
                          ),
                        ),
                      ),
                    ),
                    'composites' => 
                    array (
                      'Subscription' => 
                      array (
                        'class' => 'Subscription',
                        'local' => 'customernbr',
                        'foreign' => 'customernbr',
                        'cardinality' => 'many',
                        'owner' => 'local',
                      ),
                    ),
                    'aggregates' => 
                    array (
                      'User' => 
                      array (
                        'class' => 'modUser',
                        'local' => 'user',
                        'foreign' => 'id',
                        'cardinality' => 'one',
                        'owner' => 'foreign',
                      ),
                    ),
                  );
                    • 8386 ☆ A M B ☆
                    • 160 Posts
                    You will need to try and debug using something like PHPStorm, if you don't already have a copy there is a free 30 days evaluation version available. I think you need to step through your code bit by bit. Another option might be to use runProcessor inside of your hook.
                      • 37335
                      • 13 Posts
                      I'd love to do that, but I have no clue how I'm supposed to set that up. I'm testing everything through my websites modx install on my remote web server.. I tried to follow a video on their website, but it's not even close to that easy on the latest version.

                      Guess I'm stuck with trial and error and studying other addons until I find something I've missed. Also, I don't know how to do the runProcessor bit and everything it requires, so if someone can show me how to do it, I'd be happy to try. I've seen Quip and others use a loadController setup in the snippets, but not sure what that requires either. [ed. note: edgefive last edited this post 14 years, 4 months ago.]