We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18270
    • 68 Posts
    Hi all,
    this is probably a really simple question to answer however I cant see an example of how to do this.

    I have an extra with a database xml scheme structure. When I do an update on the extra I need to update existing tables with new columns. How do you automate this in a build script?

    I sort of assumes that if I added a line to my schema.xml, the resolver would automatically update the tables accordingly.

    Thanks!
      • 28215
      • 4,149 Posts
      Use a resolver: http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution,+Part+III#DevelopinganExtrainMODXRevolution%2CPartIII-AddingaResolver

      In it:
      if ($object->xpdo) {
          switch ($options[xPDOTransport::PACKAGE_ACTION]) {
              case xPDOTransport::ACTION_INSTALL:
              case xPDOTransport::ACTION_UPGRADE:
      $modx =& $object->xpdo;
      $modelPath = $modx->getOption('MYNAMESPACE.core_path',null,$modx->getOption('core_path').'components/MYNAMESPACE/').'model/';
      $modx->addPackage('MYNAMESPACE',$modelPath);
      $manager = $modx->getManager();
      
      $manager->addField('TableName','FieldName');
                  break;
          }
      }
      return true;
      


      The addField method is what you're looking for.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 18270
        • 68 Posts
        Ah that is great. If the table field already exists will the line
        $manager->addField('TableName','FieldName');
        simply be ignored?

        At the moment I have several upgrade installs that have added fields in each. if you are going from 1.14 to 1.16 only one is needed, however if going from 1.02 to 1.16 then about 6 new fields are needed.

        Or does it need to be more complex than this?
        e.g. if going from version A to B then add the missing field etc.

          • 18270
          • 68 Posts
          This is working really well however I get errors thrown if the field already exists when using the addField method.

          Is there a way to make addField not output errors? Or is there a better way to check if the field already exists before attempting to add?
            • 37286
            • 160 Posts
            Did you find the answer to your question? I'm getting ready to do the same thing and would like to know, thanks.
              • 3749
              • 24,544 Posts
              If you have at least one entry in the table with a known ID, you could get it with $modx->getObject().

              $object = $modx->getObject('modWhatever', $id);


              If there's at least one entry but you don't know the ID, I think this will work to get the first one, but I'm not positive:

              $object = $modx->getObject('modWhatever');


              If not, you could use $modx->getCollection() with a limit of 1 (remember that the return value for getCollection() is an array of objects, even if there's only one).

              Once you have the object, you can do this :

              $fields = $object->toArray();


              and then use in_array() to see if the field is already there before creating it.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 18270
                • 68 Posts
                Hi Bob,
                Awsome mate, I'll give the $modx->getCollection() method a try. Seems like the most robust way to do it.
                Cheers!
                  • 18270
                  • 68 Posts
                  Actually, the getCollection method is not going to work if there is no data in the table. I need to make sure that the update script will update even if the table has no data in it.

                  I was going to use the xpdo getFields method but that seems to return the fields based off the model, not based of the database structure.

                  Any other suggestions anyone?
                    • 3749
                    • 24,544 Posts
                    Did you try just using splittingred's method with @$manager->addField()?

                    If that doesn't work, it's ugly, but I guess you could write an object to the table, do the add, then delete it.


                    ------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using.
                    MODX info for everyone: http://bobsguides.com/modx.html
                      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