i have 2 tables members and comments.. i successfully joing them to. so now, i can get an collection of all entries for any user. but i need to create anothewr table called ‘followers’ , where each user can follow each. therefore, now, i want to be able to get all comments from 1 user also the comments for the users he is following too. but dont know what way to approach to complete this relationship with this new table.
users entity:
namespace Entities\Members;
/**
* @Entity(repositoryClass="\Entities\Member\MembersRepository")
* @Table(name="Members")
* @HasLifecycleCallbacks
*/
class Members extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="userid", type="bigint", length=26, nullable=true) */
protected $userid;
/** @Column(name="fname", type="string", length=255,nullable=true) */
protected $fname;
/** @OneToMany(targetEntity="\Entities\Users\Wall", mappedBy="entry", cascade={"persist"}) */
protected $commententries;
public function __construct()
{
$this->commententries = new \Doctrine\Common\Collections\ArrayCollection();
}
}
comments entity:
namespace Entities\Members;
/**
* @Entity(repositoryClass="\Entities\Member\CommentsRepository")
* @Table(name="comments")
* @HasLifecycleCallbacks
*/
class Comments extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="userid", type="bigint", length=26, nullable=true) */
/**
* @ManyToOne(targetEntity="\Entities\Member\Member", cascade={"persist"})
* @JoinColumn(name="userid", referencedColumnName="id")
*/
protected $entry;
}
this works, i can get all comments from users via:
$this->repo->findByUserid(3); (userid = 3)
however, how can i add a ‘following’ table (just like twitter). where one can follow other, and therefore, i would be able to not only, see user id-3 comments but other comments from other user he is following?
table called: followers:
namespace Entities\Members;
/**
* @Entity(repositoryClass="\Entities\Member\FollowersRepository")
* @Table(name="followers")
* @HasLifecycleCallbacks
*/
class Followers extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="userid", type="bigint", length=26, nullable=true) */
protected ???;
/**
* @Many ???
* @JoinColumn( ???
*/
protected $???;
}
Probably the best way to do this is to create a relationship from Followers to Members.
Then create a EntityRepository for the Comments Entity in which you’ll create a function like: getCommentsByUserIdIncludingFollowers($userId) in which you create a DQL query that returns the correct information/comments you need.
This would be the way I’d suggest.