Getting Started

Create your User class

The goal of this bundle is to extend FOSUserBundle powered user class with possibility to:

  • For each user unique referral code will be generated
  • Adding this referral code as a get parameter to the any site URI will store some referral information in the cookies on the client side.
  • If user who has referral cookies will register - referral information together with information about user, which referral code was used for setting cookies, will be attached to the registered user.


Only Doctrine ORM-based users from FOSUserBundle currently supported.

The bundle provides base class which is already mapped for most fields to make it easier to create your entity. Here is how you use it:

  • Extend the base User class.
  • Map the id field. It must be protected as it is inherited from the parent class.
  • Map the referralUser field.


When you extend from the mapped superclass provided by the bundle, don’t redefine the mapping for the other fields as it is provided by the bundle.

In the following section, you’ll see example of how your User class should look.

Your User class can live inside any bundle in your application. For example, if you work at “Acme” company, then you might create a bundle called AcmeUserBundle and place your User class in it.


If you override the construct() method in your User class, be sure to call parent::construct(), as the base User class depends on this to initialize some fields.

Doctrine ORM User class

Your User class should live in the Entity namespace of your bundle and look like this to start:

// src/Acme/UserBundle/Entity/User.php

namespace Acme\UserBundle\Entity;

use VEnis\Bundle\ReferralSystemDemoBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

 * @ORM\Entity
 * @ORM\Table(name="my_user")
class User extends BaseUser
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
    protected $id;

     * Referral user (his referral code was used in URI)
     * @ORM\OneToOne(targetEntity="\VEnis\DemoBundle\Entity\User")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
     * @var \FOS\UserBundle\Model\UserInterface
    protected $referralUser;

    public function __construct()
        // your own logic

Configure the FOSUserBundle

Next You must configure FOSUserBundle to use You User class and Form Handler, provided by Bundle.

# app/config/config.yml
            handler:    venis_referral_system_demo.form.handler.registration

Update your database schema

Now that the bundle is configured, the last thing you need to do is update your database schema because you have added a new entity, the User class.

Run the following command.

$ php app/console doctrine:schema:update --force

Whats next?

You now can login, logout, create users as with general FOSUserBundle. But each new user will have autogenerated unique referral code, which can be appended to any site URI as a get parameter. Site visitor, who will follow similar link and then register on the site, will be linked with user, which referral code was used. Additional referral information like Date/Ip/Referrer will be also stored.