Alternative for CollectionField for OneToOne relations
Describe the current issue This is the code in my Crud controller:
yield CollectionField::new('address', t('Address'))
->useEntryCrudForm(AddressCrudController::class)
->hideOnIndex()
->setColumns('col-sm-12');
Now, I go into the frontend, add a new address via the UI and save it. I'd expect the UI to correctly detect the OneToOne relation trough ClassMetadata, and apply the instance to the entity correctly via the given Setters.
However, an Error is thrown:
Expected argument of type "?App\Entity\AddressInterface", "array" given at property path "address".
To Reproduce
- create new entity A
- create entity B, and mark entity A to be owning side of Entity A, relation -> OneToOne
- Create Crud Controller and use adapted code as shown above
- go to UI and attempt to add new element
- save
- -> error
This leads to a situation where one would like to have a OneToOne relational field.
Suggestion
Code like this should add a form on the CRUD that will allow the user to modify the existing OneToOne relation - or fill in data if not existing yet.
yield RelationalField::new('address', t('Address'))
->useEntryCrudForm(AddressCrudController::class)
->hideOnIndex()
->setColumns('col-sm-12');
But instead of a field of inputs, only one form will be displayed.
@javiereguiluz maybe you can spotlight me the place where I'd have to look at, as I am unable yet to locate the exact place where the CollectionField get its full_name from - right now I only can generate B[id] etc fields, but not A[b][__bname__] field names, like the collection is able to.
A potential solution seems to be using a custom Type, i.e. BType that defines my fields manually. Is not what I wanted (= dynamically declare OneToOne Forms as child forms in A Crud Overview), but possible to do; however this solution now will not lead to a fully grown feature that I might commit.
For anybody stumbeling over this, as it seems not to be documented (or I just have not found it).
If you need a form inside a form, like a nested form, for OneToOne relations where you cannot use Collection, or Association (because it is something different), it is possible to call subsequent properties directly trough dot-annotation, like so:
yield TextField::new('address.street', t('Street'))
->setRequired(true)
->setColumns('col-sm-12 col-md-4');
In this case, my entity contains property address and it relates to Address object, which on its own has street property defined.