简体   繁体   中英

TYPO3 9 Extbase: edit m:n relation from both parent and child elements in backend form

In my extension I have two models: the parent model being research groups of a doctoral program, the child model being application rounds (with opening and closing dates). In an intermediate table I store the relationship between the two for research groups participating in application rounds. The application rounds can overlap with certain groups participating in two parallel rounds. That is the reason why I needed to make it an m:n relation. I made it all work using the official documentation .

Whereas I can select multiple application rounds in the research groups form, I additionally want to make the backend form of the application rounds show a list of all groups to select from.

I found the information on inline fields which shows a bidirectional use – but I am struggling translating this into my extension's situation.

Note: I do not want inline edits, really only selectCheckBox lists on both sides.

The Groups domain model contains the following property:

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Myvendor\Researchgroups\Domain\Model\ApplicationRounds>
 */
protected $applicationRounds = NULL;

persisted in the database by application_rounds INT(10)

The Groups TCA contains the following definition:

$GLOBALS['TCA']['tx_researchgroups_domain_model_groups']['columns'] = [
    'application_rounds' => [
        'label' => '' . $locLang . 'groups.recruitingSelection',
        'config' => [
            'type' => 'select',
            'renderType' => 'selectCheckBox',
            'foreign_table' => 'tx_researchgroups_domain_model_applicationrounds',
            'foreign_table_where' => 'ORDER BY tx_researchgroups_domain_model_applicationrounds.date_open DESC',
            'MM' => 'tx_researchgroups_groups_applicationrounds_mm',
            'eval' => 'int',
        ],
    ],
]

How do I add a corresponding select list to the ApplicationRounds model? Do I have to add a property there as well, persisted in the database – even thought that would be redundant?

you need to set mm-opposite-field :

If you want to make a MM relation editable from the foreign side (bidirectional) of the relation as well, you need to set MM_opposite_field on the foreign side to the field name on the local side.

[edit by original questioner] see next answer for the full code based on this answer

Here's the full working code I added following the hints in Bernd's answer :

ApplicationRounds TCA (child model)

$GLOBALS['TCA']['tx_researchgroups_domain_model_applicationrounds']['columns'] = [
    'researchgroups' => [
        'label' => '' . $locLang . 'researchgroups_applicationrounds.recruitingGl',
        'config' => [
            'type' => 'select',
            'renderType' => 'selectCheckBox',
            'foreign_table' => 'tx_researchgroups_domain_model_groups',
            'foreign_table_where' => ' AND tx_researchgroups_domain_model_groups.hidden = 0 ORDER BY tx_researchgroups_domain_model_groups.last_name ASC',
            'MM' => 'tx_researchgroups_groups_applicationrounds_mm',
            'MM_opposite_field' => 'last_name',
            'eval' => 'int',
        ],
    ],
]

(don't forget to add researchgroups to $GLOBALS['TCA']['tx_researchgroups_domain_model_applicationrounds']['types'][1] )

And add a field to the database table of the child model: researchgroups INT(11) NOT NULL DEFAULT '0',

I did not add researchgroups to the domain model though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM