We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10424
    • 4 Posts
    How can I create a category and its subcategories with a package?

    With my example the categories are created in the database
    but not with the id values I gave.

    My System parameters: Windows Vista, Modx Revolution 2.0.4, Php 5.2.8, Mysql 5.1,
    Apache 2.2, pdo_mysql is installed.

    Here is a portion of the code I use:

    
    $fields = array( 'id' => 1,'parent' => 0,'category' => 'Parent category');
    $category1 = $modx->newObject('modCategory',$fields);
    
    $fields = array( 'id' => 2,'parent' => 1,'category' => 'Subcategory 1');
    $category11 = $modx->newObject('modCategory',$fields);
                    
    $fields = array('id' => 3,'parent' => 1,'category' => ' Subcategory 2');
    $category12 = $modx->newObject('modCategory',$fields);
                    
    $fields = array('id' => 4,'parent' => 1,'category' => ' Subcategory 3');
    $category13 = $modx->newObject('modCategory',$fields);
    
    $categoryVehicleAttr = array(
        xPDOTransport::UNIQUE_KEY => 'category',
        xPDOTransport::PRESERVE_KEYS => false,
        xPDOTransport::UPDATE_OBJECT => true,
        xPDOTransport::RELATED_OBJECTS => true,
        xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
            'Children' => array(
                xPDOTransport::PRESERVE_KEYS => false,
                xPDOTransport::UPDATE_OBJECT => true,
                xPDOTransport::UNIQUE_KEY => 'category',
                xPDOTransport::RELATED_OBJECTS => true,
                xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
                    'modTemplateVar' => array(
                        xPDOTransport::PRESERVE_KEYS => false,
                        xPDOTransport::UPDATE_OBJECT => true,
                        xPDOTransport::UNIQUE_KEY => 'name',
                    )
                ),
            ),
            'modTemplateVar' => array(
                 xPDOTransport::PRESERVE_KEYS => false,
                 xPDOTransport::UPDATE_OBJECT => true,
                 xPDOTransport::UNIQUE_KEY => 'name',
            )
        )
    );
    
    $vehicle2 = $builder->createVehicle($category11,$categoryVehicleAttr);
    $builder->putVehicle($vehicle2);
    
    $vehicle2 = $builder->createVehicle($category12,$categoryVehicleAttr);
    $builder->putVehicle($vehicle2);
    
    $vehicle2 = $builder->createVehicle($category13,$categoryVehicleAttr);
    $builder->putVehicle($vehicle2);
    
    $vehicle2 = $builder->createVehicle($category1,array(  
                          xPDOTransport::PRESERVE_KEYS => false,
                          xPDOTransport::UPDATE_OBJECT => true,
                          xPDOTransport::UNIQUE_KEY => 'category'
                    ));
    
    $builder->putVehicle($vehicle2);
    
    


      • 16791
      • 5 Posts
      I am having the same problem, how do you have a subcategory under a parent category in your transport package?
        • 3749
        • 24,544 Posts
        In theory, you don’t care what the IDs are as long as they have the correct parent. Try putting the child categories in an array and adding them all to the parent category with addMany():



        <?php
        $childCategories = array();
        $category1 = $modx->newObject('modCategory',array('category'=>'ParentCategory');
        
        $fields = array( 'category' => 'Subcategory 1');
        $category11 = $modx->newObject('modCategory',$fields);
        $childCategories[] = $category11;
                        
        $fields = array('category' => ' Subcategory 2');
        $category12 = $modx->newObject('modCategory',$fields);
        $childCategories[] = $category12;
        
        $fields = array('category' => ' Subcategory 3');
        $category13 = $modx->newObject('modCategory',$fields);
        $childCategories[] = $category13;
        
        $category1->addMany($childCategories);
        
          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
          • 16791
          • 5 Posts
          I put that code in my code and used the following array for the createVehicle call

          $attr = array(
          xPDOTransport::UNIQUE_KEY => ’category’,
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::RELATED_OBJECTS => true,
          xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
          ’Snippets’ => array(
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::UNIQUE_KEY => ’name’,
          ),
          ’Chunks’ => array(
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::UNIQUE_KEY => ’name’,
          ),
          ’Children’ => array(
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::UNIQUE_KEY => ’category’,
          xPDOTransport::RELATED_OBJECTS => true,
          xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
          ’TemplateVars’ => array(
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::UNIQUE_KEY => ’name’,
          )
          ),
          ),
          ’TemplateVars’ => array(
          xPDOTransport::PRESERVE_KEYS => false,
          xPDOTransport::UPDATE_OBJECT => true,
          xPDOTransport::UNIQUE_KEY => ’name’,
          )
          ),
          );

          but I get no sub categories within the system (including db), do I still have to create and put each vehicle for each category as per above?

          Cheers,
          Luke
            • 3749
            • 24,544 Posts
            I’m not sure. You may have to put a ’Categories’ entry in the ’Children’ array. TBH, when I have trouble, I usually end up doing it in a Script resolver where I know it will work. There, you can create the category and get its ID before adding the children to it (or just set their ’parent’ fields as you create them).
              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
              At this time, to my knowledge, any levels more than 2 deep for RELATED_OBJECTS wont work. OpenGeek can give you more information on that.
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 16791
                • 5 Posts
                Thanks Bob,

                Thats exactly what I have done, I am using php resolvers to associate objects together, seemed to fix it but for the size of my package its getting pretty involved.

                Ah well thems the breaks.

                Cheers,

                Luke.