This bundle defines a Behavior for your User class to your users instances to follow each others just like inside a social network.
This document contains information on how to download, install, and start using the PacoFollowableBundle inside your Symfony project.
- Installing the Bundle
As Symfony uses Composer to manage its dependencies, the recommended way to install this bundle is to use it.
If you don't have Composer yet, download it following the instructions on http://getcomposer.org/ or just run the following command:
curl -s http://getcomposer.org/installer | php
Then, use the require command to add the bundle to your project dependencies:
php composer.phar require papepapes/pacofollowablebundle "dev-master"
Finally, enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Paco\Bundle\PacoFollowableBundle(),
);
}- Configuration
Before using the bundle, make sure that you add the proper configuration to your app/config/config.yml:
paco_followable:
# he name of the id attribute of your users table
followable_id_attribute_name: "id"
# The name of your users table
users_table_name: "users"
# The name of the joint table used for the ManyToMany relationships
joint_table_name: "follows"
# The name of the column that refers to the follower inside your joint table
joint_table_follower_column_name: "follower_id"
# The name of the column that refers to the followee inside your joint table
joint_table_followee_column_name: "followee_id"- Import the joint table definition
This bundle comes with a command paco:followable:create-table you must use to autamatically setup the joint table deinition inside your database.
Using the Symfony2 console, enter this command:
paco:followable:create-table- Using the Followable behaviour
This bundle setup a dynamic Doctrine ManyToMany relationship between your user class and itself.
It comes with:
- a trait that contains properties followers, followees and their getters/setters
- an interface to ease the usage of this behaviour to any type that implements that interface
Fisrt implements that interface and use also the trait inside your user class:
<?php
//
class User implements FollowableBehaviourInterface
{
use FollowableBehaviourTrait;
....
}Your user class have access to theses following attributes :
followers: this collection store people that followed youfollowees: this collection store people you followed and theses following methods :getFollowers(): return all your followerssetFollowers(ArrayCollection $followers)getFollowees(): returns all people you followedsetFollowees(ArrayCollection $followees)addFollower(FollowableBehaviorInterface $follower): add someone as your followeraddFollowee(FollowableBehaviorInterface $followee): follow someoneremoveFollower(FollowableBehaviorInterface $follower): remove someone as your follower. It does not delete the user.removeFollowee(FollowableBehaviorInterface $followee): unfollow someone By default when you delete a user, all relationships (not the related users) are also deleted.