Desi Cochrane
Desi Cochrane

Reputation: 657

Symfony2 determine and access subclass from superclass

I have 3 kinds of "users" that can login to my site:

  1. Staff
  2. Housing Providers
  3. Housing Seekers

I have each of these classes extend the User Master class, which implements the Symfony2 AdvancedUserInterface

/**                                                                                                                                          
 * @ORM\Entity                                                                                                                               
 * @ORM\Table(name="acme_users")                                                                                                      
 * @ORM\InheritanceType("JOINED")                                                                                                            
 * @ORM\DiscriminatorColumn(name="discr", type="string")                                                                                     
 * @ORM\DiscriminatorMap({"provider" = "ProviderUser", "seeker" = "SeekerUser", "staff" =  "StaffUser"})                     
 */
class User extends AdvancedUserInterface, \Serializable
{
    //---
}

/**                                                                                                                                          
 * @ORM\Entity                                                                                                                               
 * @ORM\Table(name="acme_seeker_users")                                                                                                      
 */
class SeekerUser extends User
{
    //---
}

/**                                                                                                                                          
 * @ORM\Entity                                                                                                                               
 * @ORM\Table(name="acme_provider_users")                                                                                                      
 */
class ProviderUser extends User
{
    //---
}

/**                                                                                                                                          
 * @ORM\Entity                                                                                                                               
 * @ORM\Table(name="acme_staff_users")                                                                                                      
 */
class StaffUser extends User
{
    //---
}

Each type of user has different properties, while inheriting some common properties of the master User class.

My question is, how can I determine and access the subclasses from the master User class?

So, I say I get the user user like so in a controller:

// get logged in user
$user = $this->get('security.context')->getToken()->getUser();

// get any user by $id
$user = $this->getDoctrine()->getRepository('acmeUserBundle:User')->findOneById($id);

How can I then determine which user it is and subsequently access that type of user's specific properties?

Or am I going about this wrong?

Upvotes: 0

Views: 621

Answers (1)

Rene Terstegen
Rene Terstegen

Reputation: 8046

A simple get_class($user) will do the trick. It will give you the name of the given class. Or $user instanceof ProviderUser (or any of the other discriminators).

Doctrine will return entities of the type you defined in your disriminator map.

Upvotes: 1

Related Questions