Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

doctrine - Symfony Form: how to set the entity being created as target for new unidirectional relation?

I have a form for an entity that is by design potentially the target of many different types of relations; let's name it ManyTargetClass. Currently this ManyTargetClass is the inverse side of those relations using bidirectional relations. I can't change the fact that this class is the target of many relations. But I thought it doesn't look very good to have a bidirectional relationship for all the possible types of relations as it pollutes my ManyTargetClass with lots of inverse-side fields as well as their getters and setters and all this.

(Or is it normal?)

I am currently mitigating this with traits to reduce the visual clutter but I am not entirely happy with it, even though there are actual use-cases for reuse of those traits.

So I thought it would be better to have my RelatingClass1, RelatingClass2,... declare a unidirectional ManyToOne-relation to ManyTargetClass to get rid of the complexity on the inverse side.

But I failed to find a way to incorporate that into my form workflow when I want to go the unidirectional route. Remark that the RelatingClassX-classes are never created on their own. They are always created via embedded forms in forms for other classes. This finally leads to my actual problem.

I want to create a ManyTargetClass-instance with embedded Forms that let me create RelationClassX-instances, automatically setting the ManyTargetClass-entity currently being created as target for the unidirectional relation established by the RelatinClassX-entity being created (preferable not having to lift potentially existing non-nullable-constraints on the join-column-definition of the RelationgClassX-side, if that is not possible I can lift this constraint though).

Is that possible? If yes: is it advisable and how would I do it?

question from:https://stackoverflow.com/questions/65841935/symfony-form-how-to-set-the-entity-being-created-as-target-for-new-unidirection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Too much code in entity

Creating many getters and setters in ManyTargetClass is nothing wrong. I have same thoughts when I started with Doctrine. You can think about entities as documentation to what is in database and what relations are there. You want to check your entity in the future and want to read what exactly it does.

Traits

That's same for traits. Don't overdo your code with traits and keep entities as simple as you can. Even the entities are huge.

Consider performance

Yours only concern should be about performance. Solution for that is unidirectional relations or fetch="EXTRA_LAZY".

More info: https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/tutorials/extra-lazy-associations.html

How to create form while using unidirectional relation

Create form with mapped=false. You can attach those entities to parent yourselves in Controller.

More info: https://symfony.com/doc/current/reference/forms/types/form.html#mapped

I'm not sure if I get your point. Provide some code examples of form and entities if my answer doesn't fit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...