Symfony2: how can I load a user based on its mapping object?

Although I use Symfony 2.1 with FOSUserBundle and everything works fine, I don’t know how I can solve the problem.

Basically, I would like to know if there is a way to find (load or receive) a user from the database, taking into account the relationship to another object.

In this situation:

I made sure that users can also log into my site through a social network (Google, Facebook, LinkedIn, etc.). In addition, I ask them to use offline access, so I can access their social accounts at any time.

Since each user can have as many connections as they need (or maybe not because they are options), I decided not to save this information in the user table. I created an abstract object called "SocialConnection", which is expanded by real ones (Google, Facebook, ect). In this entity, I store the user information that I receive when a user logs on to my site through these networks (user_id, social_id, access_token, etc.), so in my User class I mapped a collection of abstract "SocialEngine" that allows me so that all networks are united.

To be able to add other networks, such as Twitter, Yahoo, etc. in the future, I think it should be so.

So, now the problem is that when a user registers on my site through any of these social networks, I need to download it from the database, knowing his social identifier, but I don’t know how to do it.

I saw that the $ this-> findUserBy UserManager method finds the user in relation to the property, but I think it is not.

What should I do to find a good solution?

Thanks in advance.

Regards, Izzy

+4
source share
1 answer

To achieve this, you need to delve deeper into the doctrine of api. Basic repository methods (find, findBy, etc.) will not be enough.

You need to use doctrine DQL . It is similar to SQL, but you are querying based on collation metadata, not directly based on database schema.

If your user has the OneToMany $socialAccounts for SocialEngine . You can do something like:

 SELECT u FROM Bundle:User AS u JOIN u.socialAccounts AS sa WHERE sa.id = 34 

But you can only request SocialEngine properties, not subclasses of one ... (i.e. you cannot request a FacebookId on SocialFacebookEngine).

You either need to put all the twitter / facebook / etc data in the SocialEngine base class, in the User class. Or you can create an object for each "Social Engine" and create a OneToOne relationship relationship between each of these machines and the User.

+2
source

Source: https://habr.com/ru/post/1439499/


All Articles