Reputation: 367
I have the following database setup:
Tag (Integer id)
ItemTag (Integer id, Integer tag_id, Integer item_id, Integer item_type)
Artwork (Integer id)
Photo (Integer id)
I tried using the item_id field in ItemTag for both the Artwork id and Photo id like below
class ItemTag {
/**
* @Id @GeneratedValue
* @Column(type="integer")
*/
private $id;
/**
* @Column(type="integer")
*/
private $item_type;
/**
* @ManyToOne(targetEntity="Artwork")
* @JoinColumn(name="item_id", referencedColumnName="id")
*/
private $artwork;
/**
* @ManyToOne(targetEntity="Photo")
* @JoinColumn(name="item_id", referencedColumnName="id")
*/
private $photo;
/**
* @ManyToOne(targetEntity="Tag")
*/
private $tag;
}
The setup above does not produce any errors and works fine when I try to save a "photo item", however when saving an "artwork item" the item_id is NULL.
Is there a way to be able to save both relations into a single item_id field?
Upvotes: 0
Views: 1492
Reputation: 4337
Yes, you can use inheritance.
You can define an abstract class "Item" and have both Photo and Artwork extend it.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html
This requires you to change your schema slightly though.
Upvotes: 1