Automatic Deletion of Records in Child Tables

When tables are linked as child tables in MetaModels, child records are currently not automatically deleted when the corresponding parent record is deleted. While DC_General supports a “deep delete” function, this is not yet configurable for custom MM tables.

However, it can be enabled with a custom DCA configuration per parent-child relationship. This also means that deletion can be enabled for parent-child-child relationships. A file must be created for each level, which must include all levels “downward”.

The following example covers the two-level hierarchy of MM tables mm_parent with mm_child and mm_child_child. Depending on the Contao version, the DCA files must be placed in the appropriate folder — in Contao 4.9, for example, in contao/dca (see the Contao documentation).

 1 <?php
 2 // contao/dca/mm_parent.php
 3 $GLOBALS['TL_DCA']['mm_parent'] = [
 4     'dca_config' => [
 5         'data_provider'  => [
 6             'default' => [
 7                 'source' => 'mm_parent'
 8             ],
 9             'mm_child' => [
10                 'source' => 'mm_child'
11             ],
12             'mm_child_child' => [
13                 'source' => 'mm_child_child'
14             ],
15         ],
16         'childCondition' => [
17             [
18                 'from'    => 'mm_parent',
19                 'to'      => 'mm_child',
20                 'setOn'   => [
21                     [
22                         'to_field'   => 'pid',
23                         'from_field' => 'id',
24                     ],
25                 ],
26                 'filter'  => [
27                     [
28                         'local'     => 'pid',
29                         'remote'    => 'id',
30                         'operation' => '=',
31                     ],
32                 ],
33                 'inverse' => [
34                     [
35                         'local'     => 'pid',
36                         'remote'    => 'id',
37                         'operation' => '=',
38                     ],
39                 ]
40             ],
41             [
42                 'from'    => 'mm_child',
43                 'to'      => 'mm_child_child',
44                 'setOn'   => [
45                     [
46                         'to_field'   => 'pid',
47                         'from_field' => 'id',
48                     ],
49                 ],
50                 'filter'  => [
51                     [
52                         'local'     => 'pid',
53                         'remote'    => 'id',
54                         'operation' => '=',
55                     ],
56                 ],
57                 'inverse' => [
58                     [
59                         'local'     => 'pid',
60                         'remote'    => 'id',
61                         'operation' => '=',
62                     ],
63                 ]
64             ],
65         ],
66     ]
67 ];
 1 <?php
 2 // contao/dca/mm_child.php
 3 $GLOBALS['TL_DCA']['mm_child'] = [
 4     'dca_config' => [
 5         'data_provider'  => [
 6             'default' => [
 7                 'source' => 'mm_child'
 8             ],
 9             'mm_child_child' => [
10                 'source' => 'mm_child_child'
11             ],
12         ],
13         'childCondition' => [
14             [
15                 'from'    => 'mm_child',
16                 'to'      => 'mm_child_child',
17                 'setOn'   => [
18                     [
19                         'to_field'   => 'pid',
20                         'from_field' => 'id',
21                     ],
22                 ],
23                 'filter'  => [
24                     [
25                         'local'     => 'pid',
26                         'remote'    => 'id',
27                         'operation' => '=',
28                     ],
29                 ],
30                 'inverse' => [
31                     [
32                         'local'     => 'pid',
33                         'remote'    => 'id',
34                         'operation' => '=',
35                     ],
36                 ]
37             ],
38         ],
39     ]
40 ];

Unfortunately, the relation with child tables is not yet supported in frontend editing (FEE), so a custom deletion routine must be implemented there. To trigger it, the PostDeleteModelEvent from DC_General could be used. Using the ID of the deleted model, all child records with the same PID can be found and deleted.

If editing or deleting an MM item is done via a form, a deletion routine must be included there as well.