Skip to main content

Update User Entity on login

I¬†recently built an app that required we update the authenticated user’s lastOnlineDate every session so we can quickly track who has been online in the last interval of time. We set this interval as a parameter and used that to avoid hitting the database more than absolutely necessary to accomplish this.

To make it work, we needed to leverage onSecurityInteractiveLogin:

app/config/parameters.yml

MyApp/EventListener/LoginListener.php

 

app/config/services.yml


Alternatives

In this particular use-case, leveraging a setContainer call may not be necessary if all you’re doing is setting a single value on a User entity. Alternatively, we could simply send the activity_minutes parameter as an argument to the service, and not use the container at all.

We’d have to adjust our LoginListener appropriately as well:

 

Multiple dynamic EntityManagers in Symfony2

Symfony2 is fantastic when coupled with Doctrine, but unfortunately Doctrine has its limitations and does not inherently support dynamic EntityManager connections, because the parameters for each connection must be defined and loaded at the kernel level.

This assumes that each of the connections have an identical schema mapped by Doctrine2.

My live implementation functions for a single/multi tenant shared architecture in which connection parameters are encrypted and housed in MasterDatabase, while ChildDatabase1, ChildDatabase2, all have the exact same schema, but isolated data for a variety of business reasons.

There are several components to making this work:

  1. app/config/parameters.yml
  2. app/config/config.yml
  3. app/config/services.yml
  4. MyApp/Services/ApplicationConnector.php

app/config/parameters.yml

app/config/config.yml

app/config/services.yml

MyApp/Services/ApplicationConnector.php

Usage in a Controller