I can not seem to get this working. Any direction would be greatly appreciated. Below is a Snippet that I am using to test out the concept.
Here’s the structure:
- Root level category, “Top Category”
1.a Chunk related to Top Category - Child category, “Second Category”
2.a Chunk related to Second Category
<?php
$modx->loadClass('transport.modPackageBuilder','',false, true);
$builder = new modPackageBuilder($modx);
$builder->createPackage('test','0.1','alpha');
$builder->registerNamespace('test',false,true,'{core_path}components/test/');
$cat1Attr = [
xPDOTransport::UNIQUE_KEY => 'category',
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true,
xPDOTransport::RELATED_OBJECTS => true,
xPDOTransport::RELATED_OBJECT_ATTRIBUTES => [
'Children' => [
xPDOTransport::UNIQUE_KEY => 'category',
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true,
xPDOTransport::RELATED_OBJECTS => true,
xPDOTransport::RELATED_OBJECT_ATTRIBUTES => [
'Chunks' => [
xPDOTransport::UNIQUE_KEY => 'name',
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true
]
]
],
'Chunks' => [
xPDOTransport::UNIQUE_KEY => 'name',
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true
]
]
];
// Setup the first category and chunk
$cat1 = $modx->newObject('modCategory', [
'category' => 'Top Category'
]);
$chunk1 = $modx->newObject('modChunk', [
'name' => 'FirstChunk'
]);
// Add the chunk to the category
$cat1->addMany($chunk1);
// Setup the second category and chunk
$cat2 = $modx->newObject('modCategory', [
'category' => 'Child Category'
]);
$chunk2 = $modx->newObject('modChunk', [
'name' => 'SecondChunk'
]);
// Add the chunk to the category
$cat2->addMany($chunk2);
// Add the child category to the parent
$cat1->addMany($cat2);
// Create the vehicle
$vehicle = $builder->createVehicle($cat1, $cat1Attr);
$builder->putVehicle($vehicle);
// Set the attributes
$builder->setPackageAttributes(array(
'license' => "You can use it",
'readme' => "Nothing to see here"
));
When I check the resulting vehicle in the packages directory, it has the following inside the modCategory folder:
<?php return array (
'unique_key' => 'category',
'preserve_keys' => false,
'update_object' => true,
'related_objects' =>
array (
'Chunks' =>
array (
'675d3eba53ee00d9cbc9eb7ed826eb8b' =>
array (
'unique_key' => 'name',
'preserve_keys' => false,
'update_object' => true,
'class' => 'modChunk',
'object' => '{"id":null,"source":0,"property_preprocess":0,"name":"FirstChunk","description":"Chunk","editor_type":0,"category":0,"cache_type":0,"snippet":null,"locked":0,"properties":null,"static":0,"static_file":"","content":null}',
'guid' => 'ad0aaa5c8aaf60e197cae079e3230e1a',
'native_key' => NULL,
'signature' => 'f11127b591a9b7d0896edcc0390f3918',
),
),
),
'related_object_attributes' =>
array (
'Children' =>
array (
'unique_key' => 'category',
'preserve_keys' => false,
'update_object' => true,
'related_objects' => true,
'related_object_attributes' =>
array (
'Chunks' =>
array (
'unique_key' => 'name',
'preserve_keys' => false,
'update_object' => true,
),
),
),
'Chunks' =>
array (
'unique_key' => 'name',
'preserve_keys' => false,
'update_object' => true,
),
),
'namespace' => 'test',
'resolve' => NULL,
'validate' => NULL,
'vehicle_package' => 'transport',
'vehicle_class' => 'xPDOObjectVehicle',
'guid' => 'bf901ab5dd6885b236f37c2894e69b78',
'package' => 'modx',
'class' => 'modCategory',
'signature' => '71443f211b25e490a91fb3b5626e6e0e',
'native_key' => NULL,
'object' => '{"id":null,"parent":0,"category":"Top Category","rank":0}',
);
Only the chunk directly related to the top level category is packaged in. I saw reference to Category Closures. Do I need to somehow package those in as well?
If I try and create a vehicle for each category, the files are created and chunks are referenced, but it doesn’t install correctly since the relationship is missing between categories.
Do most/all Extras just have a single category?