Reputation: 83
My database schema:
LIKE PAGE ARTICLE
id id id
userid name text
itemid
LIKE.itemid -> PAGE.id "or" ARTICLE.id
Normally, i use this way;
Page.php;
/**
* @ORM\OneToMany(targetEntity="Like", mappedBy="page")
*/
protected $likes;
Article.php;
/**
* @ORM\OneToMany(targetEntity="Like", mappedBy="article")
*/
protected $likes;
Like.php;
/**
* @ORM\ManyToOne(targetEntity="Page", inversedBy="likes")
* @ORM\JoinColumn(name="itemid", referencedColumnName="id")
*/
protected $page;
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="likes")
* @ORM\JoinColumn(name="itemid", referencedColumnName="id")
*/
protected $article;
But, But, itemid column will be the same in both tables (page and article). Will not be realized.
The question; I want to add a condition. Like this;
LIKE PAGE ARTICLE
id id id
userid name text
itemid
**itemtype**
and
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="likes")
* @ORM\JoinColumn(name="itemid", referencedColumnName="id", **condition="itemtype=1"**)
*/
protected $article;
like this way, etc.
What should I do?
Upvotes: 1
Views: 587
Reputation: 137182
You probably want to create a supertype, say Document
, from which Article
and Page
can inherit. Then your Like
s can relate to Document
s.
Doctrine2 ORM supports three types of inheritance: Mapped Superclass, Single Table, and Class Table. Picking the one you need will require some background on your usage requirements.
Read the Design-time considerations, Performance impact, and SQL Schema considerations of each option on the documentation page linked above to figure out which is right for you.
Upvotes: 2