Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with setting the default value for a ManyToOne relation but only when used in livecomponent with livecollection #2225

Open
gewait opened this issue Sep 30, 2024 · 0 comments

Comments

@gewait
Copy link

gewait commented Sep 30, 2024

I have an entity "Main" which has a OneToMany relation named "children" to the entity "Child".

class Main
{
    #[ORM\OneToMany(targetEntity: Child::class, mappedBy: 'main')]
    private Collection $children;

Inside the "Child" there is a ManyToOne relation named "other" to another entity named "Related".

class Child
{
    #[ORM\ManyToOne]
    private ?Related $other = null;

When a new child is created I want to set a default value for the relation "other".

I can't do it in the constructor of the entity because I would need the entitymanager for that.

I can't use a factory class, because I want to use this in a livecomponent with a livecollection
and there the creation of the objects is done internally.

So I do it via the data option in the formtype for child:

    src/Form/ChildType.php

    $builder
        ->add('other', EntityType::class, [
            'required' => false,
            'class' => Related::class,
            'choice_label' => 'name',
            'data' => $this->relatedRepository->findOneByName('when_this_is_preselected_then_it_works'),  //set default
        ])

This does work in a normal form

but does not work in a live component with a live collection.


Since the live collection does all the work internally when creating/adding a new child,
I don't know how or where I can initialize the child.other relation property!


Is this the expected behaviour? What am I doing wrong? Can someone help?


Reproducer:

live-collection-reproducer-zip.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant