Same column for two different relational entities with doctrine
Well, I have these three tables:
likes
id_like | post | post_type
post: it has the post id where the "like button" was triggered.
post_type: it has the type of the post, it could "article" or "comment".
Comment
id_comment | body
Article
id_article | body
I am trying to create the models "Article" and "Comment" where each one
can has an array of its likes. For example, I have tried this with:
Model Like
/**
* @Entity
* @Table(name="likes")
*/
class Like
{
/**
* @Id @Column(type="integer", nullable=false, name="id_like")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="integer", nullable=false, name="like_type")
*/
protected $type;
Model Article
namespace models;
use \Doctrine\Common\Collections\ArrayCollection;
/** * @Entity * @Table(name="article") */ class Article {
/**
* @Id @Column(type="integer", nullable=false, name="id_article")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @OneToMany(targetEntity="Vote", mappedBy="article")
* @JoinColumn(name="id_article", referencedColumnName="post")
*/
protected $likes;
My first problem comes when I try to get the "likes" from the article. If
I do:
$likes = $article->getLikes();
I get this error:
Severity: Notice
Message: Undefined index: article
Filename: Persisters/BasicEntityPersister.php
Line Number: 1574
Now, the second problem (or question) that eventually will come is about
the "Comment" model. Is be possible do a OneToMany from Comment to Likes
using as the same way as Article do?
No comments:
Post a Comment