We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25586
    • 105 Posts
    Hi all!

    I've tried for hours but I've finally decided to ask the forums!

    I'm busy with a personal project and I'm trying to save data to my table by using a hook.

    This is the snippet.

    <?php
    $focus = $modx->getService('focus','Focus',$modx->getOption('core_path').'components/focus/model/focus/',$scriptProperties);
    
    $user = $focus->getUserObj($focus->userID);
    
    $location = $modx->newObject('Location', $hook->getValues());
    
    $user->addMany($location);
    
    if($user->save()) {
    	return true;
    } else {
    	$hook->addError('error_message','Could not save data');
    	return false;
    }


    It's working perfect without FormIt but when I change the code to work with FormIt does nothing. The page submits the form and gives it's succes message but when I check my database nothing is added.

    Anyone know how to solve is?

    Could it be that I'm working with Revolution 2.2? [ed. note: rover87 last edited this post 12 years, 4 months ago.]
      • 25586
      • 105 Posts
      I just installed Revolution v2.1 and the same problem occurs sad.

      Stange thing is that when I remove the getValues and hook parts and call it from a page with $location->fromArray(array('address' => 'Street 1')); The data gets added to the database!

      Anyone any idea what I'm doing wrong?
        • 25586
        • 105 Posts
        I also find it strange that formIt outputs the succesMessage, even though nothing is saved.
          • 25586
          • 105 Posts
          I found out that when I use $location->set or $location->fromArray I get a 500 server error on submitting. I think it could be a bug in the FormIt snippet?

          Hope someone can help me out.

          Regards,

          Rogier
            • 28215
            • 4,149 Posts
            Is your "Focus" class running $modx->addPackage to add your focus package to the loaded maps?
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 25586
              • 105 Posts
              Yeah it's running fine when I call the snippet in a page, the data gets added to the table. But when I use it as a hook it fails.

              Do you have any idea what this could be? Anyone else experienced this problem?

              Regards,

              Rogier
                • 25586
                • 105 Posts
                I made some progress on the problem! I stopped my last project but now I have the same thing with an other project I'm working on.

                I have the following resource:
                
                [[!FormIt? &hooks=`[[++crt.core_path]]elements/snippets/snippet.addName.php`]]
                
                <form action="[[~[[*id]]]]" method="post" class="form">
                    <label for="name">
                        Name: <input type="text" name="name" id="name" value="[[!+fi.name]]" />
                    </label>
                    <input type="submit" value="Add" />
                </form>


                Not to fancy for testing purpose. Here is the code of the addName snippet
                <?php
                
                /* Activate the class */
                $x = $modx->getService('crt','Crt',$modx->getOption('crt.core_path',null,$modx->getOption('core_path').'components/crt/').'model/crt/',$scriptProperties);
                
                /* Check if the class is instanced */
                if (!($x instanceof Crt)) return 'Unable to load service.';
                
                $userId = $modx->getObject('extUser', $modx->user->get('id'));
                
                $location = $modx->newObject('Location',  array(
                		'name' => $hook->getValue('name')
                ));
                
                $userId->addMany($location);
                
                if($userId->save()) {
                	return true;
                } else {
                	return false;
                }


                When I fill in the form nothing happens.

                But when I add the following snippet to the resource, it works perfectly!
                [[!include? &file=`[[++crt.core_path]]elements/snippets/snippet.crt.php`]]


                Contents
                <?php
                
                /* Activate the class */
                $x = $modx->getService('crt','Crt',$modx->getOption('crt.core_path',null,$modx->getOption('core_path').'components/crt/').'model/crt/',$scriptProperties);
                
                /* Check if the class is instanced */
                if (!($x instanceof Crt)) return 'Unable to load service.';


                The only reason I can think of is that the formIt snippet can't find the class needed. I hope someone has a answer for this strange problem!

                Regards,

                Rogier
                  • 25586
                  • 105 Posts
                  I found out that I can completely remove the
                  $x = $modx->getService('crt','Crt',$modx->getOption('crt.core_path',null,$modx->getOption('core_path').'components/crt/').'model/crt/',$scriptProperties);
                  line from the hook snippet when I call the include snippet.

                  Do I need the getService call? I have followed the extUser tutorial, maybe I have a error in my class but everything is working fine. Here is the file to be sure.
                  <?php
                  
                  if (!class_exists('Crt')) {
                      class Crt
                  	{
                  	
                  		/* Setting up global parameters */
                  	    public $modx;
                  	    public $config = array();
                  	
                  	    function __construct(modX &$modx,array $config = array()) {
                  	    
                  	    	/* Import modx as a reference */
                  	        $this->modx =& $modx;
                  	 		
                  	 		/* Define base & assets path */
                  			$basePath = $this->modx->getOption('crt.core_path',$config,$this->modx->getOption('core_path').'components/crt/');
                  			$assetsUrl = $this->modx->getOption('crt.assets_url',$config,$this->modx->getOption('assets_url').'components/crt/');
                  	        
                  	        /* Establish the environment */
                  	        $this->config = array_merge(array(
                  	            'basePath' => $basePath,
                  	            'corePath' => $basePath,
                  	            'modelPath' => $basePath.'model/',
                  	            'processorsPath' => $basePath.'processors/',
                  	            'chunksPath' => $basePath.'elements/chunks/',
                  	            'jsUrl' => $assetsUrl.'js/',
                  	            'cssUrl' => $assetsUrl.'css/',
                  	            'assetsUrl' => $assetsUrl,
                  	            'connectorUrl' => $assetsUrl.'connector.php',
                  	            'userID' => $this->modx->user->get('id'),
                  	        ),$config);
                  	       
                  			/* Call the crt package */
                  	        $this->modx->addPackage('crt',$this->config['modelPath']);
                  	
                  	        /* Define the user */
                  	        $this->userObj = $this->setUser($this->config['userID']);
                  	        $this->userID = $this->userObj->get('id');
                  	    }
                  	 
                  	    function __destruct() {
                  	        unset ($this->userObj, $this->userID, $this->config);
                  	    }
                  	    
                  	    public function getUserObj($userID) {
                  	        return $this->modx->getObject('extUser', $userID);
                  	    }
                  	    
                  	    public function setUser($userID){
                  	        return $this->getUserObj($userID);
                  	    }
                  	    
                  		public function getChunk($name,$properties = array()) {
                  		    $chunk = null;
                  		    if (!isset($this->chunks[$name])) {
                  		        $chunk = $this->_getTplChunk($name);
                  		        if (empty($chunk)) {
                  		            $chunk = $this->modx->getObject('modChunk',array('name' => $name));
                  		            if ($chunk == false) return false;
                  		        }
                  		        $this->chunks[$name] = $chunk->getContent();
                  		    } else {
                  		        $o = $this->chunks[$name];
                  		        $chunk = $this->modx->newObject('modChunk');
                  		        $chunk->setContent($o);
                  		    }
                  		    $chunk->setCacheable(false);
                  		    return $chunk->process($properties);
                  		}
                  		 
                  		private function _getTplChunk($name,$postfix = '.chunk.tpl') {
                  		    $chunk = false;
                  		    $f = $this->config['chunksPath'].strtolower($name).$postfix;
                  		    if (file_exists($f)) {
                  		        $o = file_get_contents($f);
                  		        $chunk = $this->modx->newObject('modChunk');
                  		        $chunk->set('name',$name);
                  		        $chunk->setContent($o);
                  		    }
                  		    return $chunk;
                  		}
                  	}
                  }


                  I hope someone can help me out!

                  Kind regards,

                  Rogier
                    • 3749
                    • 24,544 Posts
                    I think addMany() wants an array. Try something like this:

                    $userId->addMany(array($location));


                    Also, to pick a nit, it should probably be $userObj not $userId to avoid confusing yourself at some later point.

                      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
                      • 25586
                      • 105 Posts
                      Hi Bob,

                      Thanks for your reply, I tried your solution but the problem still exists. When I try the code without formIt, it works, but with formIt it fails. So it looks like formIt breaks my getService call somehow. And thank you for the hint, will definitely use userObj instead of userId!

                      So if someone has an idea why my getService fails, I really would like to know!

                      Regards,

                      Rogier

                      p.s. I tried it with MODx 2.0, MODx 2.1 and now with MODx 2.2rc-1-3