typo3typo3-12.xdoctrine-dbal

Hydrating objects in n:1-relationships TYPO3


Inside my Extbase extension, I have two classes ParentUser and Child.

The child table holds a reference to the parent_user

CREATE TABLE tx_xxx_domain_model_child (
    ...
    parent_user int(11) unsigned DEFAULT '0'
)

The parent_user table doesn't know about the Child. Is it possible to hydrate the ParentUser object in such a way, that it holds a ObjectStorage (or similar) of children. I want to render a list of all ParentUsers with its children.

Note, I don't want the model to know about any repository. It would be nice to use the ProppertyMapper.


Some more Code:

TCA:

...
'parent_user' => [
    'exclude' => false,
    'label' => 'LLL:EXT:xxx/Resources/Private/Language/locallang_db.xlf:tx_xxx_domain_model_child.parent_user',
    'description' => 'LLL:EXT:xxx/Resources/Private/Language/locallang_db.xlf:tx_xxx_domain_model_child.parent_user.description',
    'config' => [
        'type' => 'select',
        'renderType' => 'selectSingle',
        'foreign_table' => 'tx_xxx_domain_model_parentuser',
        'default' => 0,
        'minitems' => 0,
        'maxitems' => 1,
    ],
],

CREATE TABLE tx_xxx_domain_model_child (
    first_name varchar(255) DEFAULT NULL,
    last_name varchar(255) DEFAULT NULL,
    date_of_archive int(11) NOT NULL DEFAULT '0',
    state int(11) NOT NULL DEFAULT '0',
    parent_user int(11) unsigned DEFAULT '0'
);

CREATE TABLE tx_xxx_domain_model_parentuser (
    first_name varchar(255) DEFAULT NULL,
    last_name varchar(255) DEFAULT NULL,
    email varchar(255) DEFAULT NULL,
    city varchar(255) DEFAULT NULL,
    zip varchar(255) DEFAULT NULL,
    region varchar(255) DEFAULT NULL,
    country varchar(255) DEFAULT NULL,
    address varchar(255) DEFAULT NULL,
    date_of_archive int(11) NOT NULL DEFAULT '0',
    state int(11) NOT NULL DEFAULT '0'
);

Using:


Solution

  • Yes that's possible.

    Your Parent needs a field childs (Or similar).

    /**
     * @var ObjectStorage<Child>|null
     */
    #[Lazy()]
    public ObjectStorage|null $childs;
    

    In the ext_tables.sql you add that field to your Parent table:

    childs    int(11) unsigned DEFAULT '0' NOT NULL,
    

    And in the TCA of your Parent you can add it like this:

        'childs' => [
            'label' => 'Childs',
            'config' => [
                'type' => 'inline',
                'foreign_table' => 'tx_xxx_domain_model_child',
                'foreign_field' => 'parent_user',
            ],
        ],