We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    If I use $obj->set(’category’,$category_name); and the category doesn’t exist already, the build and install go smoothly with no errors but the category isn’t created and the elements are assigned to category 0.


    If I create the category first, I get an error message in the build but the category is created. The elements are still assigned to category 0.

    I’m not sure if this is a bug or if I’m doing something wrong.

    Here’s the code I’m using to create the category:

    $c= $modx->newObject('modCategory');
    $c->set('name','spform');
    $c->set('category','spform');
    
    $attributes= array( XPDO_TRANSPORT_UNIQUE_KEY => 'name', XPDO_TRANSPORT_UPDATE_OBJECT => true, XPDO_TRANSPORT_PRESERVE_KEYS => false );
    $vehicle = $builder->createVehicle($c, $attributes);
    $builder->putVehicle($vehicle);


      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
      • 28215
      • 4,149 Posts
      BobRay,

      You’ll actually want to do this via related objects, since other than that there’s no way for MODx to know the end-result PK values of the category and relate them to the tables.

      For example, here’s how I’ve done it in a test component that includes 3 snippets.

      $attr = array(
          XPDO_TRANSPORT_UNIQUE_KEY => 'category',
          XPDO_TRANSPORT_PRESERVE_KEYS => false,
          XPDO_TRANSPORT_UPDATE_OBJECT => true,
          XPDO_TRANSPORT_RELATED_OBJECTS => true,
          XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array (
              'modSnippet' => array (
                  XPDO_TRANSPORT_PRESERVE_KEYS => false,
                  XPDO_TRANSPORT_UPDATE_OBJECT => true,
                  XPDO_TRANSPORT_UNIQUE_KEY => 'name',
              )
          )
      );
      $category= $modx->newObject('modCategory');
      $category->set('category','TestComponent');
      
      $snippets = array();
      for ($i=1;$i<4;$i++) {
          $c= $modx->newObject('modSnippet');
          $c->set('id',$i);
          $c->set('name', 'Snippet'.$i);
          $c->set('description', 'Test '.$i);
          $c->set('snippet', file_get_contents($sources['root'] . 'test/elements/snippet'.$i.'.class.php'));
      
          $snippets[] = $c;
      }
      $category->addMany($snippets,'modSnippet');
      
      $vehicle = $builder->createVehicle($category,$attr);
      $vehicle->resolve('file',array(
          'source' => $sources['root'] . 'test',
          'target' => "return MODX_ASSETS_PATH . 'components/';",
      ));
      $builder->putVehicle($vehicle);
      


      For different types of related objects, you’d just add different definitions for each one, and then addMany each different group of them. Does that make sense?

      Hope that helps.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3749
        • 24,544 Posts
        Thanks! That’s a great example.

        It makes my loop a little more complicated since there are snippets, chunks, and resources in it together but it should be easy to put each one into a separate array based on the object_type.
          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
          • 3749
          • 24,544 Posts
          It seems to be working. I had to do $categoryObject->set(’id’,nn); with unique ids for each bundle of elements or only one got through (presumably that will change with the new code).

          Here’s my code. Let me know if there’s an easier way:

          /* This sets the snippets' categories using the $snippet array  */
          $i++;
          $attr = array(
              XPDO_TRANSPORT_UNIQUE_KEY => 'category',
              XPDO_TRANSPORT_PRESERVE_KEYS => false,
              XPDO_TRANSPORT_UPDATE_OBJECT => true,
              XPDO_TRANSPORT_RELATED_OBJECTS => true,
              XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array (
                  'modSnippet' => array (
                      XPDO_TRANSPORT_PRESERVE_KEYS => false,
                      XPDO_TRANSPORT_UPDATE_OBJECT => true,
                      XPDO_TRANSPORT_UNIQUE_KEY => 'name',
                  )
              )
          );
          
          $categoryObject= $modx->newObject('modCategory');
          $categoryObject->set('category','spform');
          $categoryObject->set('id', $i);
          $categoryObject->addMany($snippets,'modSnippet');
          
          $vehicle = $builder->createVehicle($categoryObject,$attr);
          $builder->putVehicle($vehicle);
          
          /* Now we'll set the chunks' category using the $chunks array */
          $i++;
          $attr = array(
              XPDO_TRANSPORT_UNIQUE_KEY => 'category',
              XPDO_TRANSPORT_PRESERVE_KEYS => false,
              XPDO_TRANSPORT_UPDATE_OBJECT => true,
              XPDO_TRANSPORT_RELATED_OBJECTS => true,
              XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array (
                  'modChunk' => array (
                      XPDO_TRANSPORT_PRESERVE_KEYS => false,
                      XPDO_TRANSPORT_UPDATE_OBJECT => true,
                      XPDO_TRANSPORT_UNIQUE_KEY => 'name',
                  )
              )
          );
          
          $categoryObject= $modx->newObject('modCategory');
          $categoryObject->set('category','spform');
          $categoryObject->set('id', $i);
          $categoryObject->addMany($chunks,'modChunk');
          
          $vehicle = $builder->createVehicle($categoryObject,$attr);
          $builder->putVehicle($vehicle);
          


          BTW, I don’t think the $attr array is something a person could puzzle out on their own. wink

            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