We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49846
    • 22 Posts
    I am completely baffled by this. I have two tables: info_k1 and info_k2. I am using FormIt plugin to create a form and then a snippet to create newObjects to store the results into table K1. I am to successful in submitting my results into K1 and populating K2 with the details of the user such as u_id and email. When user proceed to fill in the second form, the snippet used to getObject (to specify which user/row to update) works, as I am able to retrieve the data from K2. However when I try to set the field for results using the xPDO set object, it does not write to the table. I am baffled by this as my code is almost identical to the code that works for R1. And it's not like it's unable to connect to the table as I am able to retrieve objects from it. I don't think there is anything wrong with my schema. I was able to write to the table of K2 to begin with so I have no idea why it isn't allowing me to update the row when I am able to for K1. Furthermore, when I change the table from K2 in the snippet that fails to write to K2 to K1, it successfully writes to K1.

    Has anyone come across a similar problem?? Or have any idea where I am going wrong?
      • 49846
      • 22 Posts
      I am able to create a newObject and have it write a new row in the table. But I cannot update the current row. :/ I just don't understand because it worked for the other table
        • 40706
        • 128 Posts
        Sounds like something with your PrimaryKey definition isn`t correct. This would result something like you describe.
          • 49846
          • 22 Posts
          Quote from: m.engel at Feb 24, 2015, 07:15 AM
          Sounds like something with your PrimaryKey definition isn`t correct. This would result something like you describe.

          Thank you for your reply. Here is a section of schema. Table k1 and k2 are the same. Can you guide me with how to correct the PrimaryKey definition?

          <object class="K1" table="k1" extends="xPDOSimpleObject">
          <field key="u_id" dbtype="int" precision="11" phptype="integer" null="false" />
          <field key="company" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
          <field key="cohort" dbtype="varchar" precision="50" phptype="string" null="false" default="" index="index" />
          <field key="name" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
          <field key="email" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
          <field key="code" dbtype="varchar" precision="50" phptype="string" null="false" />
          <field key="results" dbtype="text" phptype="string" null="false" />
          <field key="status" dbtype="int" precision="1" phptype="integer" null="false" default="0" />
          <field key="timestamp" dbtype="timestamp" phptype="timestamp" null="false" default="CURRENT_TIMESTAMP" />

          <index alias="cohort" name="cohort" primary="false" unique="false" type="FULLTEXT" >
          <column key="cohort" length="" collation="" null="false" />
          </index>
          </object>
            • 3749
            • 24,544 Posts

            In order to update the object, you have to get it first. Are you testing the value of getObject()? Are you saving the changed object?

            For reference, here is what the code might look like:

            $id = 12; // or whatever
            $obj = $modx->getObject('K1', array('u_id' => $id));
            if ($obj) {
                $obj->set('status', 1);
                $obj->save();
            
            } else {
               return 'Could not get Object with id: ' . $id;
            
            }


              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
              • 49846
              • 22 Posts
              Hello Bob. Thank you for your reply.

              Yes I have done that. And it has successfully worked in other snippets. Like I've mentioned, I am able to write to K2 to begin with, but then I am unable to update the row with getObject, set and save. So I have something like this:

              $friend2 = $modx->getObject('K2', array(

              'id' => $id,
              ));
              $email = $friend2->get('email');
              $friend2->set('status', 1);
              $modx->log(modX::LOG_LEVEL_ERROR, $email );
              $friend2->save();

              So I am able to get the email from K2 and it prints out the email log in the error log. And the code does run i.e. the rest of the code after this executes. But the table does not update. Status does not set to 1. I think it may be because of my database? I know my code works.... I have no idea why it's not updating my rows in the table. :S K1 updates. Both have populated data. But K2 won't update. However I am able to add another row with newObject. This is baffling. I hope someone can help me! I am intrigued to find out what the problem is. Thanks
                • 3749
                • 24,544 Posts
                I would try repairing the table. The only other things I can think of: maybe your schema doesn't have the K2 object extending either xPDOObject or xPDOSimpleObject, or maybe you've overridden the save() function in your class files.
                  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
                  • 49846
                  • 22 Posts
                  But if K2 isn't exetending the xPDOOBject etc... then the data wouldn't be able to populate the table in the first place and I wouldn't be able to getObject and print out the values? What do you mean by "overridden the save() function in your class files"? I am able to save a new object to the table...? Sorry I am a beginner to MODX... Do you reckon it's something to do with my keys or schema, where I have the update function locked i.e. can overwritten existing data in the table.
                    • 3749
                    • 24,544 Posts
                    It's a mystery all right. I think everything you're saying is correct. If you don't know what I mean by "overriding the save() function" it's almost certain that you haven't done it.

                    Did you try repairing the table?

                    About the only thing left that I can think of is mod_security not liking something you're trying to save in the K2 object, but it seems pretty unlikely.
                      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
                      • 49846
                      • 22 Posts
                      Sorry for the late reply. I have already tried that but no luck. Really have no idea. Thanks anyway though. smiley