We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44231
    • 126 Posts
    hi there!

    How do I update tables in my custom table?

    i saw a thread about this but it only confuses me.

    thanks in advance!

    modx revo 2.2.6 pl traditional
      • 4172
      • 5,888 Posts
      some more infos for us?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44231
        • 126 Posts
        ok sorry for the vague information smiley

        im creating some kind of form maintenance for my website serves it handles adding(insert) of data and modify(update) of data.

        but im stuck in updating the old data of my custom tables.

        im using xpdo query.

        here is my code

         //store the path for displaying the image
        						$ads_img = $_FILES["image_upload"]["name"];
                                $ads_img = addslashes($ads_img);
        
                                $filename = $_FILES["image_upload"]["name"];
                                
                               
                                /*insert query*/
                                
                                $bid = $_POST['brand_id'];
                                $bshotid = $_POST['brandshots_id'];
                                $tagS = $filename;
                                $tagI = $filename;
                                $EB = $_POST['entered_by'];
                                $DE = date('Y-m-d / H:i:s');
                                $MB = $_POST['entered_by'];
                                $DM = date('Y-m-d / H:i:s');
                                
                                $sql =$modx->query("INSERT INTO mod_brandshots (system_brandshots_id, system_brand_id, tagshots, tagimages, entered_by, date_entered, modified_by, date_modified)VALUES
                                ('$bid','$bshotid','$tagS','$tagI','$EB','$DE','$MB','$DM')");
                                
                                if(isset($_POST['update_upload'])){
                                    (update query here)
                                }


        the values is coming from a form method post.

        thanks!
          • 3749
          • 24,544 Posts
          Have you created a schema and class and map files for you custom table so it can be used with xPDO? There's information on how to do that here: http://bobsguides.com/custom-db-tables.html.

          If so, all you need is to get the object you want to update with $modx->getObject() and then do something like this:


          $object->set('fieldName', $value);
          /* more of the same */
          
          $object->save();
            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
            • 44231
            • 126 Posts
            thank you for the reply

            but this code give this errors

            Notice: Undefined variable: object in C:\tfv\public_html\core\cache\includes\elements\modsnippet\40.include.cache.php on line 104
            
            Fatal error: Call to a member function set() on a non-object in C:\tfv\public_html\core\cache\includes\elements\modsnippet\40.include.cache.php on line 104
              • 4172
              • 5,888 Posts
              I think you forgot to get the object into $object.

              If so, all you need is to get the object you want to update with $modx->getObject() and then do something like this:
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 44231
                • 126 Posts
                getObject?meaning the table? or the fields?
                  • 4172
                  • 5,888 Posts
                  http://rtfm.modx.com/display/xPDO20/Using+Your+xPDO+Model
                  http://rtfm.modx.com/display/xPDO20/Retrieving+Objects

                  if ($object = $modx->getObject('yourObjectClass',$yourObjectID)){
                      $object->set('fieldName', $value);
                      /* more of the same */
                   
                      $object->save();
                  }
                  else{
                      //error handling, couldn't get the object
                  }
                  
                  


                  you can also just try to use formit + formit2db
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 44231
                    • 126 Posts
                    thanks for the reply but I did not update my current table data
                         if ($object = $modx->getObject('Brandshots',$bid)){
                                             $object->set('system_brandshots_id', $bshotid);
                                             $object->set('tagshots', $tagS);
                                             $object->set('tagimages', $tagI);
                                             $object->set('entered_by', $EB);
                                             $object->set('date_entered', $DE);
                                             $object->set('modified_by', $MB);
                                             $object->set('date_modified', $DM);
                                             $object->save(); 
                                        }
                                        else{
                                            echo'error';
                                        }
                                                
                      • 3749
                      • 24,544 Posts
                      getObject() gets a single row in the table (which xPDO treats as an object).

                      Are you sure $bid is set to the id of an actual table row? Are you getting the error message? Does your table have an 'id' field?

                      getObject() can be called to get the object either by name or by ID:

                      $object = $modx->getObject('Brandshots', $id);
                      $object = $modx->getObject('Brandshors', array('name' => 'SomeObjectName'));  // change 'name' to the actual field name
                      


                      (Actually, with the second version, you can get the object by any field name that will contain a unique value.)
                        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