We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39941
    • 5 Posts
    I'm running Revo 2.2.4 and MyComponent 1.0.0 beta1

    I packaged some chunks, snippets and templates with the very useful mycomponents scriptset but two things in the resolver I cannot figure out (I'm new to XPDO):

    • setting parents for resources
    • attaching the new resources with the new templates

    The parents:
    I have 4 resources:
    One of it is an empty container called 'css',
    and three resources of type text/css that shall be installed as the children 'style', 'smallscreen' and 'bigscreen'.

    The templates:
    These css-resources shall get a template attached to them.

    In a resolver (e.g.
    mycomponents/PKG_NAME/_build/resolvers/install.script.php
    ) in the section beginning with
    case xPDOTransport::ACTION_INSTALL:
    how would I do it?

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      You need something like this:

      <?php
      $parentResource = $modx->getObject('modResource', array('pagetitle' => 'something'));
      
      $childResource = $modx->getObject('modResource', array('pagetitle' => 'somethingelse'));
      
      $template = $modx->getObject('modTemplate', array('templatename' => 'something'));
      if ($template) {
         $templateId = $template->get('id');
      } else {
          $modx->log(MODX::LOG_LEVEL_ERROR, 'Error getting template');
      }
      
      if ($parentResource && $childResource) {
      
          $parentId = $parentResource->get('id');
          $childResource->set('parent', $parentId);
          $childResource->set('template', $templateId);
          $childResource->save();
      
      } else {
          $modx->log(MODX::LOG_LEVEL_ERROR, 'Error getting resources');
      }
      




      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 2 months ago.]
        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
        • 39941
        • 5 Posts
        Thank you very much!
        But I had no luck with with the getObject()-call on modResource (and even modPropertySet) neither with 'pagetitle' nor 'alias'. The resources and everything else were all transported properly and criteria string-values are correct and in correct case.
        This is the code:
        [...]
            case xPDOTransport::ACTION_INSTALL:
        //$modx->reloadContext('web');
                $parentResource = $modx->getObject('modResource', array('pagetitle' => 'css'), false);
                if ($parentResource) {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Retrieved css');
                } else {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Could not get css');
                }
                $childResource = $modx->getObject('modResource', array('pagetitle' => 'style'), false);
                if ($childResource) {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Retrieved style');
                } else {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Could not get style');
                }
                $template = $modx->getObject('modTemplate', array('templatename' => 'automatCSSTemplate'), false);
                if ($template) {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Retrieved automatCSSTemplate');
                } else {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Could not get automatCSSTemplate');
                }
                if ($template) {
                    $templateId = $template->get('id');
                } else {
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Error getting template');
                }
                if ($parentResource && $childResource){
                    $parentId = $parentResource->get('id');
                    $childResource->set('parent', $parentId);
                    $childResource->set('template', $templateId);
                    $childResource->save();
                } else{
                    $modx->log(MODX::LOG_LEVEL_ERROR, 'Error getting resources');
                }
        [...]

        The following is what I got:
        /*
        * MODX Console Output
        *
        * @date 2012-07-27 13:06:38
        */
        Attempting to install package with signature: automat-1.0.1-beta5
        Package found...now preparing to install.
        Grabbing package workspace...
        Workspace environment initiated, now installing package...
        Running PHP Resolver.
        Could not get css
        Could not get style
        Retrieved automatCSSTemplate
        Error getting resources
        Script resolver actions completed
        Successfully installed package automat-1.0.1-beta5
        /* EOF */

        Finding the issue in an older post of yours here
        https://forums.modx.com/thread/73277/getobject-in-script-resolver-no-longer-works?page=2#dis-post-431332 pointed me to inserting the
        reloadContext('web');
        with no luck. The installer-window was just empty but let me proceed. But it did not work. No entries in core/cache/logs files were added.
        I haven't found this method is member of the XPDO class.
        Did I put it in the wrong location? (call is commented out in the above code)
        I too disabled the global modx-cache and still no luck.
        Pointed to the cache I found the third parameter of getObject() being a cache-flag (found here: http://rtfm.modx.com/display/xPDO20/xPDO.getObject) and set it still with no luck.

        I made two new clean modx-installs to also check with 777 file-permissions.
        I'm on openSUSE 12.1 and so it is not an XAMPP issue. [ed. note: webknot last edited this post 14 years, 2 months ago.]
          • 3749
          • 24,544 Posts
          I've had that problem on and off for while. The most likely issue is that the resolvers actually run in the order they are added to the package (I used to think they ran after the installation is finished and it says that -- incorrectly -- in my book).

          That means you need to make sure you add the resources in the build script before you add the resolver that gets them. The next version of MyComponent will add the resources at the top of the build script.

          The reloadContext() call is in the right place (anywhere before the getObject() call is fine), and you may still need it (but probably not).

          On occasion, I've set the resource parent to 0 and put the code to set the parent into a some snippet in the package rather than the resolver and had the code run the first time the snippet is called, but I don't think that's necessary any more.

          The current version of my build scripts has this code near the top:

          /* Transport Resources */
          
          if ($hasResources) {
              $resources = include $sources['data'] . 'transport.resources.php';
              if (!is_array($resources)) {
                  $modx->log(modX::LOG_LEVEL_ERROR, 'Could not package in resources.');
              } else {
                  $attributes = array(
                      xPDOTransport::PRESERVE_KEYS => false,
                      xPDOTransport::UPDATE_OBJECT => false,
                      xPDOTransport::UNIQUE_KEY => 'pagetitle',
                      xPDOTransport::RELATED_OBJECTS => true,
                      xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array(
                          'ContentType' => array(
                              xPDOTransport::PRESERVE_KEYS => false,
                              xPDOTransport::UPDATE_OBJECT => true,
                              xPDOTransport::UNIQUE_KEY => 'name',
                          ),
                      ),
                  );
                  foreach ($resources as $resource) {
                      $vehicle = $builder->createVehicle($resource, $attributes);
                      $builder->putVehicle($vehicle);
                  }
                  $modx->log(modX::LOG_LEVEL_INFO, 'Packaged in ' . count($resources) . ' resources.');
              }
              unset($resources, $resource, $attributes);
          }



          ------------------------------------------------------------------------------------------
          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
            • 39941
            • 5 Posts
            That's it!
            I moved it to a point near the top (right after the category is created) and everything is working.
            Relieved and happy!
            You made my day!
              • 3749
              • 24,544 Posts
              Yes, but I wrecked some of your days first by not doing things right in MyComponent. wink


              I'm glad you got it sorted.



              ------------------------------------------------------------------------------------------
              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