We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    I am trying to update a single field in a single record I can only figure out 2 possible ways of doing this and neither seem to work.
    The ways I have tried are :-
    1
    <?php
    $query = $modx->newQuery('DBClass');
    $query->where(array(
      'ID' => '12',
    ));
    $record = $modx->getObject('DBClass',$query);
    
    $record->fromArray(array(
      'FieldtoUpdate' => $_POST['field'],
    ));
    $record->save();
    ?>


    2
    <?php
    $record = $modx->newObject('DBClass');
    $record->fromArray(array(
      'ID' => '12', /* Where this is an exsiting ID */
      'FieldtoUpdate' => $_POST['field'],
    ));
    $record->save();
    ?>


    Any help on this would be appreciated.
      • 13481
      • 97 Posts
      try

      $record->set('FieldtoUpdate',$variable);
      $record->save();


      fromArray() should work, but I’ve had a similar issue where fromArray() didn’t work, but set() did.

      James
        • 3749
        • 24,544 Posts
        You may also be able to do the getObject() call in one line:

        $record = $modx->getObject('DBClass',12);


        I know it works with resources.
          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
          • 34193
          • 330 Posts
          Quote from: devtrench at Jul 27, 2010, 09:18 PM


          $record->set('FieldtoUpdate',$variable);
          $record->save();


          Thanks for the suggestion James still no look though, a little bit more info for folks, I ran this script step by step in debug mode and checked the contents of $record at each point the record is being retrieved correctly the ->set or ->fromArray both set the field to the required value in $record but the $record->save() does not seem to be updating these to the actual DB.

          I have no problems creating a new record, so the system can write to the DB but I can’t get it to update existing records.

          Pete
            • 28215
            • 4,149 Posts
            Can you post your custom schema for this package?

            Also, are you calling $modx->addPackage before trying to do this?
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 34193
              • 330 Posts
              Hi yes happily the snippets I’d used above I’d generalized the names on so won’t match exctly whats here but here is the custom schema and the whole of my code.
              <?xml version="1.0" encoding="UTF-8"?>
              <model package="VLE" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM"
                   phpdoc-package="VLE" phpdoc-subpackage="">
              
                  <object class="VLEPupil" table="VLE_Pupils" extends="xPDOObject">
                      <field key="RoleNum"    dbtype="int"     precision="11" phptype="integer" null="false" default="" index="index"/>
                      <field key="UPN"        dbtype="varchar" precision="13" phptype="string" null="true" default="" />
                      <field key="FirstName"  dbtype="varchar" precision="30" phptype="string" null="false" default="" />
              	<field key="MiddleName" dbtype="varchar" precision="30" phptype="string" null="true" default="" />
              	<field key="SurName"    dbtype="varchar" precision="30" phptype="string" null="false" default="" />
              	<field key="DOB"        dbtype="date"    precision=""  phptype="date" null="false" default="" />
              	<field key="Gender"     dbtype="varchar" precision="1" phptype="string" null="false" default="" />
              	<field key="YearGroup"  dbtype="int"     precision="11" phptype="integer" null="true" default="" />
              	<field key="Group"      dbtype="varchar" precision="2"  phptype="string" null="true" default="" />
              	<field key="House"      dbtype="varchar" precision="6"  phptype="string" null="true" default="" />
              	<field key="Religion"   dbtype="varchar" precision="20" phptype="string" null="true" default="" />
              	<field key="HomeLang"   dbtype="varchar" precision="20" phptype="string" null="true" default="" />
              	<field key="Status"     dbtype="varchar" precision="8"  phptype="string" null="false" default="" />
              
                       <composite alias="VLEPupilParentsLink" class="VLEPupilParent" local="RoleNum" foreign="PupilRoleNum" cardinality="many" owner="local" />
                  </object>
              
                  <object class="VLEParent" table="VLE_Parents" extends="xPDOObject">
                      <field key="ParentId"   dbtype="int"     precision="11" phptype="integer" null="false" default="" index="index"/>
                      <field key="title"      dbtype="varchar" precision="10" phptype="string" null="false" default="" />
                      <field key="FirstName"  dbtype="varchar" precision="30" phptype="string" null="false" default="" />
                      <field key="SurName"    dbtype="varchar" precision="30" phptype="string" null="false" default="" />
              	<field key="MobileNum"  dbtype="varchar" precision="11" phptype="string" null="true" default="" />
              	<field key="LandNum"    dbtype="varchar" precision="11" phptype="string" null="true" default="" />
              	<field key="Address1"   dbtype="varchar" precision="30" phptype="string" null="true" default="" />
              	<field key="Address2"   dbtype="varchar" precision="30" phptype="string" null="true" default="" />
              	<field key="Address3"   dbtype="varchar" precision="30" phptype="string" null="true" default="" />
              	<field key="PostCode"   dbtype="varchar" precision="8"  phptype="string" null="true" default="" />
              	<field key="Priority"   dbtype="int"     precision="11" phptype="integer" null="false" default="" />
                      <field key="email"      dbtype="varchar" precision="50" phptype="string" null="false" default="" />
                      <composite alias="VLEParentsPupil" class="VLEPupilParent" local="ParentId" foreign="ParentRoleNum" cardinality="many" owner="local" />
                  </object>
              
                  <object class="VLEPupilParent" table="VLE_Pupils-Parent" extends="xPDOObject">
                      <field key="PupilRoleNum"   dbtype="int" precision="11" phptype="integer" null="false" default="" />
                      <field key="ParentRoleNum"  dbtype="int" precision="11" phptype="integer" null="false" default="" />
              
                      <aggregate alias="Pupil" class="VLEPupil" local="PupilRoleNum" foreign="RoleNum" cardinality="one" owner="foreign" />
                      <aggregate alias="Parent" class="VLEParent" local="ParentRoleNum" foreign="ParentId" cardinality="one" owner="foreign" />
                  </object>
              </model>

              <?php
              function generate_UPN($role) {
                  $leaNo = '893';
                  $establishmentNo = '4567';
                  $year = date('y');
                  $UPN =  $leaNo . $establishmentNo . $year . $role;
                  $UPNsingle = str_split($UPN, 1);
                  $currentchar = 2 ;
                  $total = 0 ;
                  foreach ($UPNsingle as $character) {
                      $total += (int)$character*$currentchar;
                      $currentchar = $currentchar+1;
                  }
                  $remainder = $total % 23;
              
                  switch ($remainder) {
                      case 0:
                          $UPN = 'A'.$UPN;
                          break;
                      case 1:
                          $UPN = 'B'.$UPN;
                          break;
                      case 2:
                          $UPN = 'C'.$UPN;
                          break;
                      case 3:
                          $UPN = 'D'.$UPN;
                          break;
                      case 4:
                          $UPN = 'E'.$UPN;
                          break;
                      case 5:
                          $UPN = 'F'.$UPN;
                          break;
                      case 6:
                          $UPN = 'G'.$UPN;
                          break;
                      case 7:
                          $UPN = 'H'.$UPN;
                          break;
                      case 8:
                          $UPN = 'J'.$UPN;
                          break;
                      case 9:
                          $UPN = 'K'.$UPN;
                          break;
                      case 10:
                          $UPN = 'L'.$UPN;
                          break;
                      case 11:
                          $UPN = 'M'.$UPN;
                          break;
                      case 12:
                          $UPN = 'N'.$UPN;
                          break;
                      case 13:
                          $UPN = 'P'.$UPN;
                          break;
                      case 14:
                          $UPN = 'Q'.$UPN;
                          break;
                      case 15:
                          $UPN = 'R'.$UPN;
                          break;
                      case 16:
                          $UPN = 'T'.$UPN;
                          break;
                      case 17:
                          $UPN = 'U'.$UPN;
                          break;
                      case 18:
                          $UPN = 'V'.$UPN;
                          break;
                      case 19:
                          $UPN = 'W'.$UPN;
                          break;
                      case 20:
                          $UPN = 'X'.$UPN;
                          break;
                      case 21:
                          $UPN = 'Y'.$UPN;
                          break;
                      case 22:
                          $UPN = 'Z'.$UPN;
                          break;
                  }
                  return $UPN;
              
              }
              
              if (isset($_POST['subit'])) {
              
                  $base_path = MODX_BASE_PATH.'assets/VLE/core/components/vle/';
                  /* change above line to your path */
              
                  $modx->addPackage('VLE',$base_path.'model/');
              
                  /*Initial record creation */
                  $pupil = $modx->newObject('VLEPupil');
                  $pupil->fromArray(array(
                          'UPN' => $_POST['upn'],
                          'FirstName' => $_POST['firstname'],
                          'MiddleName' => $_POST['othernames'],
                          'SurName' => $_POST['surname'],
                          'DOB' => $_POST['DOB'],
                          'Gender' => $_POST['gender'],
                          'YearGroup' => $_POST['YG'],
                          'Group' => $_POST['group'],
                          'House' => $_POST['house'],
                          'Religion' => $_POST['religion'],
                          'HomeLang' => $_POST['homelang'],
                          'Status' => $_POST['status'],
                  ));
                  $pupil->save();
                  
                  
                  /*Check if UPN was given if not then create one and update record */
                  if (empty($_POST['upn'])) {
                      /*Generate UPN here */
                      $query = $modx->newQuery('VLEPupil');
                      $query->select('LAST_INSERT_ID()');
                      $lastkey = $modx->getObject('VLEPupil',$query);
                      $rolenum = $lastkey->get('LAST_INSERT_ID()');
              
                      $UPN = generate_UPN($rolenum);
                      
                      $query = $modx->newQuery('VLEPupil');
                      $query->where(array(
                              'RoleNum' => $rolenum,
                      ));
                      
                      /*this bit not working */
                      $pupilupdate = $modx->getObject('VLEPupil',$query);
              
                      $pupilupdate->set('UPN',$UPN);
              
                      $pupilupdate->save();
              
                  } else {
                      /* Validate UPN here */
                  }
              } else {
                  echo $modx->getChunk ('addPupilFrm');
              }
              
              ?>


              it’s the bit below the if (empty($_POST[’upn’])) {
              /*Generate UPN here */

              That doesn’t work, if I enter a UPN in the form it is saved as part of the record creation, it’s the second save where the UPN is generated if it doesn’t already exisit that is the problem
                • 28215
                • 4,149 Posts
                You need to specify a Primary Key for your table; you haven’t done so.

                If you want it to be an auto-generated id, change extends="xPDOObject" to extends="xPDOSimpleObject", delete the classes and maps for VLEPupil, and regenerate them.

                If you want the PK to be the RoleNum field, just change ’index="index"’ in the field definition to ’index="pk"’.
                  shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                  • 34193
                  • 330 Posts
                  Thanks Shaun you guys are just the best smiley