@halftrainedharry,
I created the schema as you showed me and then i created the snippets “post_aftergetfields” and “post_aftersave” but i have problem in the backend of migxdb. The multiple document types values of the posts are showing but when i select the values are not saving in the database.
This is my CMP of "document_types_categories"
`{
"formtabs":[
{
"MIGX_id":2,
"caption":"Info",
"print_before_tabs":"0",
"fields":[
{
"MIGX_id":6,
"field":"name_gr",
"caption":"Name (Greek Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":1
},
{
"MIGX_id":7,
"field":"name_en",
"caption":"Name (English Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":2
},
{
"MIGX_id":8,
"field":"name_tr",
"caption":"Name (Turkish Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":3
},
{
"MIGX_id":9,
"field":"published",
"caption":"Published",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"checkbox",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"Yes==1",
"default":1,
"useDefaultIfEmpty":1,
"pos":4
}
],
"pos":1
}
],
"contextmenus":"update||duplicate||publish||unpublish||remove",
"actionbuttons":"addItem",
"columnbuttons":"",
"filters":"",
"extended":{
"migx_add":"Add Document Type",
"disable_add_item":"",
"add_items_directly":"",
"formcaption":"",
"update_win_title":"",
"win_id":"document_types_categories",
"maxRecords":"",
"addNewItemAt":"top",
"media_source_id":"",
"multiple_formtabs":"",
"multiple_formtabs_label":"",
"multiple_formtabs_field":"",
"multiple_formtabs_optionstext":"",
"multiple_formtabs_optionsvalue":"",
"actionbuttonsperrow":4,
"winbuttonslist":"",
"extrahandlers":"",
"filtersperrow":4,
"packageName":"posts_lists",
"classname":"Document_Type_Category",
"task":"",
"getlistsort":"",
"getlistsortdir":"",
"sortconfig":"",
"gridpagesize":25,
"use_custom_prefix":"0",
"prefix":"",
"grid":"",
"gridload_mode":1,
"check_resid":1,
"check_resid_TV":"",
"join_alias":"",
"has_jointable":"yes",
"getlistwhere":"",
"joins":"",
"hooksnippets":"",
"cmpmaincaption":"Document Types",
"cmptabcaption":"Table",
"cmptabdescription":"Manage your Document Types here. You can edit them by right-clicking on the respective row.",
"cmptabcontroller":"",
"winbuttons":"",
"onsubmitsuccess":"",
"submitparams":""
},
"columns":[
{
"MIGX_id":1,
"header":"ID",
"dataIndex":"id",
"width":"",
"sortable":true,
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":2,
"header":"Name GR",
"dataIndex":"name_gr",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":3,
"header":"Name EN",
"dataIndex":"name_en",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":4,
"header":"Name TR",
"dataIndex":"name_tr",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":5,
"header":"Published",
"dataIndex":"published",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"this.renderCrossTick",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
}
],
"category":""
}`
And this is my CMP of "posts_lists"
`{
"formtabs":[
{
"MIGX_id":20,
"caption":"Info",
"print_before_tabs":"0",
"fields":[
{
"MIGX_id":101,
"field":"name_gr",
"caption":"Name (Greek Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":1
},
{
"MIGX_id":102,
"field":"name_en",
"caption":"Name (English Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":2
},
{
"MIGX_id":103,
"field":"name_tr",
"caption":"Name (Turkish Language)",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"",
"useDefaultIfEmpty":"0",
"pos":3
},
{
"MIGX_id":104,
"field":"medium_id",
"caption":"Medium Category",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"listbox",
"validation":"required",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"@EVAL return $modx->runSnippet('migxLoopCollection',array('packageName'=>'medium_categories','classname'=>'Medium_Category','tpl'=>'@CODE:[[+name_gr]]==[[+id]]','outputSeparator'=>'||'));",
"default":"",
"useDefaultIfEmpty":"0",
"pos":4
},
{
"MIGX_id":105,
"field":"Document_Type_Categories",
"caption":"Document Type",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"listbox-multiple",
"validation":"required",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"@EVAL return $modx->runSnippet('migxLoopCollection',array('packageName'=>'posts_lists','classname'=>'Document_Type_Category','tpl'=>'@CODE:[[+name_gr]]==[[+id]]','outputSeparator'=>'||'));",
"default":"",
"useDefaultIfEmpty":"0",
"pos":5
},
{
"MIGX_id":106,
"field":"published",
"caption":"Published",
"description":"",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"checkbox",
"validation":"",
"configs":"",
"restrictive_condition":"",
"display":"",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"Yes==1",
"default":1,
"useDefaultIfEmpty":1,
"pos":6
}
],
"pos":1
}
],
"contextmenus":"update||duplicate||publish||unpublish||remove",
"actionbuttons":"addItem",
"columnbuttons":"",
"filters":"",
"extended":{
"migx_add":"Add Post",
"disable_add_item":"",
"add_items_directly":"",
"formcaption":"",
"update_win_title":"",
"win_id":"posts_lists",
"maxRecords":"",
"addNewItemAt":"top",
"media_source_id":"",
"multiple_formtabs":"",
"multiple_formtabs_label":"",
"multiple_formtabs_field":"",
"multiple_formtabs_optionstext":"",
"multiple_formtabs_optionsvalue":"",
"actionbuttonsperrow":4,
"winbuttonslist":"",
"extrahandlers":"",
"filtersperrow":4,
"packageName":"posts_lists",
"classname":"posts_List",
"task":"",
"getlistsort":"",
"getlistsortdir":"",
"sortconfig":"",
"gridpagesize":25,
"use_custom_prefix":"0",
"prefix":"",
"grid":"",
"gridload_mode":1,
"check_resid":1,
"check_resid_TV":"",
"join_alias":"",
"has_jointable":"yes",
"getlistwhere":"",
"joins":"",
"hooksnippets":{
"aftergetfields":"post_aftergetfields",
"aftersave":"post_aftersave"
},
"cmpmaincaption":"Posts",
"cmptabcaption":"Table",
"cmptabdescription":"Manage your Posts here. You can edit them by right-clicking on the respective row.",
"cmptabcontroller":"",
"winbuttons":"",
"onsubmitsuccess":"",
"submitparams":""
},
"columns":[
{
"MIGX_id":1,
"header":"ID",
"dataIndex":"id",
"width":"",
"sortable":true,
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":2,
"header":"Name GR",
"dataIndex":"name_gr",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":3,
"header":"Name EN",
"dataIndex":"name_en",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":4,
"header":"Name TR",
"dataIndex":"name_tr",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":6,
"header":"Medium",
"dataIndex":"medium_id",
"width":"",
"sortable":1,
"show_in_grid":1,
"customrenderer":"",
"renderer":"this.renderChunk",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"[[migxLoopCollection? &packageName=`medium_categories` &classname=`Medium_Category` &tpl=`@CODE:{{+name_gr}}` &where=`{\"id:=\":\"[[+medium_id]]\"}`]]",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":7,
"header":"Document Type",
"dataIndex":"Document_Type_Categories",
"width":"",
"sortable":1,
"show_in_grid":1,
"customrenderer":"",
"renderer":"this.renderChunk",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"[[migxLoopCollection? &packageName=`posts_lists` &classname=`Document_Type_Category` &tpl=`@CODE:{{+name_gr}}` &where=`{\"id:=\":\"[[+Document_Type_Categories]]\"}` &outputSeparator=`||`]]",
"renderoptions":"",
"editor":""
},
{
"MIGX_id":5,
"header":"Published",
"dataIndex":"published",
"width":"",
"sortable":"false",
"show_in_grid":1,
"customrenderer":"",
"renderer":"this.renderCrossTick",
"clickaction":"",
"selectorconfig":"",
"renderchunktpl":"",
"renderoptions":"",
"editor":""
}
],
"category":""
}`
And these are my snippets
“post_aftergetfields”
`<?php
$base_path = $modx->getOption('core_path').'components/posts_lists/';
$modx->addPackage('posts_lists', $base_path.'model/');
if ($object) {
$record_fields = $object->get('record_fields');
$post_id = $record_fields['object_id'];
$team_ids = array();
$result = $modx->getCollection('post_document_type', array('post_id' => $post_id));
foreach($result as $res){
$team_ids[] = $res->get('document_type_id');
}
$record_fields['posten'] = implode("||",$team_ids);
$object->set('record_fields',$record_fields);
}
return '';`
“post_aftersave”
`<?php
$base_path = $modx->getOption('core_path').'components/posts_lists/';
$modx->addPackage('posts_lists', $base_path.'model/');
$object = & $modx->getOption('object',$scriptProperties,null);
$post_id = $object->get('id');
//query everything that is already saved in the database
$teams_old = array();
$result= $modx->getCollection('post_document_type', array('post_id ' => $post_id ));
foreach($result as $res){
$teams_old[] = $res->get('document_type_id');
}
$str_teams = $object->get('posten');
$teams_new = explode('||',$str_teams);
foreach($teams_new as $team_id){
if ($team_id === ''){
//no teams selected for this trainer
break;
}
$team_id = (int)$team_id;
$pos = array_search($team_id, $teams_old);
if (false !== $pos){
//team-id is already saved in the database
unset($teams_old[$pos]);
} else {
//add new team-id
$new_t = $modx->newObject('post_document_type');
$new_t->set('document_type_id',$team_id);
$new_t->set('post_id ',$post_id );
$new_t->save();
}
}
//delete remaining team-ids from the database
foreach($teams_old as $team_id){
$entry = $modx->getObject('post_document_type',array('post_id ' => $post_id, 'document_type_id' => $team_id));
$entry->remove();
}`