0 votes

When i try to create a one-to-one uni-directional relationship between two entities, skipper's Associations dialog shows only bi-directional option.

How can i tell to skkipper that "Hey, there are no reference from second entity to first entity. This is uni-directional relationship"

Related documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-unidirectional

asked in How To & Manuals by (220 points)
recategorized by

2 Answers

+1 vote
Best answer

Hi Fiya,

as Martin wrote in his answer, uni-directional assocation (@OneToMany or @ManyToOne) is configured through aliases.

As you can see in following examples, based on these aliases you can control how your association should looks like. It's because Doctrine2 handles associations based on the aliases.

In all next examples I will use following model:

enter image description here

In case you fill only owner alias, you will get unidirectional (one-way) association heading from child to parent. This is what you probably need. As export, you will get:

 @ORM\ManyToOne(targetEntity="First")
 @ORM\JoinColumn(name="first_id", referencedColumnName="id")

Skipper one-way association

In case you fill only inverse alias, you will get incorrect unidirectional (one-way) association heading from parent to child. I'm writing incorrect, because this isn't correct Doctrine2 configuration and based on the ERD modelling it's not possible to create such association without helper table. In this situation you will get:

 @ORM\OneToMany(targetEntity="Second")

Skipper one-way association

In case you fill both aliases, you get bidirectional association (two-way) between child and parent. In this case, Skipper exports annotations to both entities:

 SecondEntity:
 @ORM\ManyToOne(targetEntity="First")
 @ORM\JoinColumn(name="first_id", referencedColumnName="id")

 FirstEntity:
 @ORM\OneToMany(targetEntity="Second")

Skipper two-way association

Hope these examples will help you understand the concept of creating association on our application.

answered by Skipper developer (133k points)
selected by

Thanks! I figured out that. But definitely assoc. wizard needs some tweaks to visualise this. I confused because of Association Type dropdown always shows one-to-many. It may disabled or shown as text (like Direction) on some cases like this. Thanks again.

It's not possible to disable association type dropdown because you still need to have a way how to switch between one-to-many and one-to-one association type. It's difference between "Association type" and "Association Direction".

We're always trying to make GUI as simple as possible, but one-to-many association can be uni-directional or bi-directional as same as one-to-one association can be uni/bi-directional too, so both GUI elements are required here.

0 votes

To create unidirectional relationship, you just simply omit one of the aliases. If only one alias is defined, the association will be exported to definitions as unidirectional.

answered by Skipper developer (74.9k points)

Thank, i understand now. But it still forces relationship to one-to-many and i just figured out that i need a many-to-one. How can change relationship type? Combobox in Associations dialog has only 2 options: one-to-many and one-to-one.

If I understand correctly what you need to do, then the difference between one-to-many and many-to-one is from the model view only in the selection of the owner and inverse entity, switch the entities, and you will switch the direction of the relation.

You can change the entities in the wizard, or if you are using the GUI tool, just click the entities in the reverse order.

Martin, no. I'm talking about many-to-one relationship like this example. Switching entities nor direction doesn't producing a @ManyToOne annotation for example.