Reputation: 440
I have got 4 entities (Address, User, Contact, Account). Every record in User, Contact and Account can have many Addresses. What I have done is:
/**
* Address
*
* @ORM\Table(name="address")
* @ORM\Entity
*/
class Address
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="join_id", type="integer")
*/
private $joinId;
/**
* @var string
*
* @ORM\Column(name="join_type", type="string", length=16)
*/
private $joinType;
........
}
So as join_type I am saving USER, CONTACT or ACCOUNT and as join_id I am saving the ID of related record in User, Contact and Account entity.
Is there a way to do this somehow using relations, so I don't need to run extra queries to get Address and it would be easier to saving this?
Upvotes: 0
Views: 52
Reputation: 742
I guess the Doctrine Single Table Inheritance (http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html#single-table-inheritance) is exactly what you need. So, you'll have one top-hierarchy entity - Address & 3 extending entities for each of your relation - UserAddress, ContactAddress, AccountAddress. Just define all common properties at Address entity, relations definitions move to inheriting entities.
Upvotes: 4