Question

Using Doctrine 2 and Symfony here. I'm fairly new to Doctrine and can't find what I'm looking for in their docs, so I'm looking for some guidance on a situation.

I have these entities: 1. Profile (profile of a person, a person can have many addresses) 2. School (school info, a school can have many addresses) 3. Address (all addresses, associated to one of the above entities)

Previously, I had Profile, ProfileAddress, School, SchoolAddress, which worked fine, but I'd rather have a single class/table to make address updates to because I may get other entities down the road which also need addresses. Also, if my address format changes, I just want to change ONE class/table instead of many.

My question is this... Is it possible to associated two (or more) different entities to a single entity? Here is an example of the data I would EXPECT to see in the actual tables.

-Profiles-

|id|fname |lname|
| 1|John  |Smith|
| 2|Jane  |Jones|

-Schools-

|id|name             |type             |
| 1|City College     |University       |
| 2|Greendale        |Community College|

-Addresses-

|id|src_id|src     |type    |address1          |address2|
| 1|     1|profiles|maling  |PO BOX 555        |        |
| 2|     1|profiles|physical|123 W. Main St.   |Apt A   |
| 3|     1|schools |physical|541 University Way|        |
| 4|     2|schools |physical|111 Winger Ave    |        |

Is this possible with Doctrine? If so HOW? Or is there a better way I could design this? What have others done in this situation?

Was it helpful?

Solution 2

Looks like I just didn't know the correct words to use. I found the documentation for Doctrine's MappedSuperClass and/or single table inheritance which has what I need.

Documentation here: http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html

OTHER TIPS

It's quite possible, you just need 2 mapping tables, so instead of having a source column in addresses, you would have a profile_address and school_address tables. You would need to specify unidirectional (one way) going from profile to address and school to address.

Edit:

in the standard example of giving a user roles you would have:

/**
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 */
protected $roles;

now lets say you wanted to have another object, let's say bystander that you wanted to have roles for as well, you would have:

/**
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="bystander_role",
 *     joinColumns={@ORM\JoinColumn(name="bystander_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 */
protected $roles;

that's all there is to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top