We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37286
    • 160 Posts
    I am working on my first addon that will be open to the public and I am getting frustrated with the modx-combo-user in the manager. This is a modx 2.2 install.

    My create window code:
    UG.window.CreateUserSetting = function(config) {
        config = config || {};
        Ext.applyIf(config,{
            title: _('ug.create')
            ,url: UG.config.connectorUrl
            ,baseParams: {
                action: 'mgr/usersettings/create'
            }
            ,fields: [{
                xtype: 'modx-combo-user'
                ,fieldLabel: _('ug.user')
                ,name: 'g_user'
    	    ,hiddenName: 'g_user'
                ,anchor: '100%'
            },{
                xtype: 'textfield'
                ,fieldLabel: _('ug.title')
                ,name: 'g_title'
                ,anchor: '100%'
            },{
                xtype: 'textarea'
                ,fieldLabel: _('ug.description')
                ,name: 'g_description'
                ,anchor: '100%'
            }]
        });
        UG.window.CreateUserSetting.superclass.constructor.call(this,config);
    };
    Ext.extend(UG.window.CreateUserSetting,MODx.Window);
    Ext.reg('ug-window-usersettings-create',UG.window.CreateUserSetting);
    


    What happens is the combo box appears fine and it populates with users. When I click the save button it either saves as a "0" in the database or errors out, like it's not passing the value from the combo box. The part that is frustrating me is that I already built another part of the mgr using the modx-combo-usergroup and it worked perfectly.

    Clip of what I did with modx-combo-usergroup to work in my create window:
            ,fields: [{
                xtype: 'modx-combo-usergroup'
                ,fieldLabel: _('ug.usergroup')
                ,name: 'usergroup'
    	    ,hiddenName: 'usergroup'
                ,anchor: '100%'
            },{
    


    Any ideas?
      • 28215
      • 4,149 Posts
      Did you try inspecting the sent request to see what exactly it's sending? That code looks right - I don't see why it wouldn't send the correct value. Try debugging further in your processor.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 37286
        • 160 Posts
        I didn't think about it, but it may be a problem with my schema. I checked the error logs and noticed something I didn't see the first time through:

        my error:
        INSERT INTO `xxx_ug_usersettings` (`g_title`, `g_description`) VALUES ('', '')
        Array
        (
        [0] => 23000
        [1] => 1062
        [2] => Duplicate entry '0' for key 1
        )

        but I don't see it trying to insert my g_user field. Any idea why it would just ignore my primary field?

        excerpt from my schema
        	<!-- user settings -->
            <object class="UGUserSettings" table="ug_usersettings" extends="xPDOObject">
        		<field key="g_user" dbtype="int" precision="11" attributes="unsigned" phptype="integer" null="false" index="pk" />
        		<field key="g_title" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
        		<field key="g_description" dbtype="text" phptype="string" null="false" default="" />
        
        		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
        			<column key="g_user" collation="A" null="false" />
        		</index>
        		<aggregate alias="modUser" class="modUser" local="g_user" foreign="id" cardinality="one" owner="foreign" />
        		<aggregate alias="UGA" class="UGA" local="g_user" foreign="owner" cardinality="many" owner="local" />
            </object>
        
          • 28215
          • 4,149 Posts
          I would have to see the PHP code you're doing to create the object.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 37286
            • 160 Posts
            ok, well I changed to xpdosimpleobject so the user is no longer my primary key and everything works the way its supposed to. I don't know why that makes a difference when saving the user id number to a primary key or just another field, but It works now with an extra column I don't need.

            As for my php:
            class UGUserSettingsCreateProcessor extends modObjectCreateProcessor {
                public $classKey = 'UGUserSettings';
                public $languageTopics = array('ug:default');
                public $objectType = 'ug.usersettings';
             
                public function beforeSave() {
                    
            		return parent::beforeSave();
                }
            }
            return 'UGUserSettingsCreateProcessor';
            


            I haven't set up any of the rules yet, still working on the layout.

            BTW, do you know if the modObjectCreateProcessor class would work on the front end web context, or is it a function for use in the mgr context only?
              • 28215
              • 4,149 Posts
              I'm not 100% sure about it working in the web context; I would assume it would, since it's in the processor layer of the MODX request, which is separate from the controller layer that the mgr runs in. You could always try. I believe it should work.

              Secondly, the reason your test doesn't work with the PK not being the ID field is because modObjectCreateProcessor only calls this to set it's fields:

              $this->object->fromArray($this->getProperties());


              fromArray, without the 3rd parameter being true, will not set the primary key field. You'll have to add this within your beforeSave clause to get it to work:

                  public function beforeSave() {
                       $this->object->set('g_user',$this->getProperty('g_user'));
                      return parent::beforeSave();
                  }

                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 37286
                • 160 Posts
                That works for creating a record, thank you.

                Now, update does not work, I tried to use that same beforeSave action in the hope that would work, but I get a "Not specified." error.
                  • 37286
                  • 160 Posts
                  Found the solution, had to add "primaryKeyField":

                  class UGUserSettingsUpdateProcessor extends modObjectUpdateProcessor {
                      public $classKey = 'UGUserSettings';
                      public $languageTopics = array('ug:default');
                      public $objectType = 'ug.usersettings';
                      public $primaryKeyField = 'g_user';
                  	
                  
                  }
                  return 'UGUserSettingsUpdateProcessor';