2

I am currently searching for a way to share users between multiple Symfony2 applications for one of our customers. In the past 3 years, we made a few applications (4) for them that have different purposes, but always use the "same" user model and data.

Currently, we have 4 separate databases, where the "users" table is kinda the same on all the applications, except for the many-to-many relationships. At first, I was thinking about adding a second entity manager (and connection), and putting the users in a separate database. All the applications would be able to use this and all the users would have the same credentials. But how do I handle the many-to-many relationships?

To give an example, on application A you have a many-to-many relation from "Users" to "Clients", but the "Clients" table doesn't exist in application B/C/D. On application B, you have a many-to-many relation from "Users" to "Suppliers", but the "Suppliers" table doesn't exist in application A/C/D and so on. Moving the "Clients" or "Suppliers" table to the shared database isn't really an option either, because other entities (which are not shared) are also having relations to those tables.

I basically need to find a way to map many-to-many relationships on the "shared user" model/database which are unique for each application. Is there a way to achieve this with multiple databases? Should I go for some other approach?

All info is welcome. Thanks in advance.

mwinter
  • 596
  • 2
  • 5
  • 14
  • first thing that i think is that it is not mandatory to use Doctrine's relation mapping which is designed for database relations only. You can also fill an array manually inside an entity. Most of the times you will need an UserManager (or UserHelper) class that you add to the service container. Then you can do something like $user = userHelper->loadByUsername($username); or userHelper->update($user); – Frank B Nov 29 '16 at 19:35

1 Answers1

3

Option 1

Using different connections, this doesn't seem to be possible with Doctrine out of the box .

Similar questions have been answered already:

As stated in the first answer, you could do the following:

keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the objects through services.

But if your want Doctrine to actually be aware of the associations, you need to persist the associated entities in the same database, or your mapping will just generate errors. That means you would need to duplicate the User entity.

Option 2

In the very specific case where you can use the same connection (that is, with multiple databases of the same DBMS on the same host and with the same user), there seems to be a way, but I haven't tested it: https://techpunch.co.uk/development/using-multiple-databases-with-symfony2-and-doctrine2

The idea is to prefix each table with the database name, as if it were a schema name, like this:

This entity is mapped to the «User» table in the database «users»:

<?php
namespace Demo\UserBundle\Entity;
use DoctrineORMMapping as ORM;

/**
 * @ORMTable(name="users.User")
 */
class User
{
  /* ... */
}

This one is mapped to the «Post» table in the database «posts»:

<?php
namespace Demo\PostBundle\Entity;
use DoctrineORMMapping as ORM;

/**
 * @ORMTable(name="posts.Post")
 */
class Post
{
  /* ... */
}

Then you can make associations as usual:

class Post
{
    /**
     * @ORM\ManyToOne(targetEntity="\Demo\UserBundle\Entity\User")
     **/
    private $user;
    /* ... */
}

The author also links to an example project on github: https://github.com/lobsterdore/symfony2-multiple-db-example

Community
  • 1
  • 1
vctls
  • 736
  • 7
  • 25
  • Thumbs up for that last link; just tried it out locally and it works perfect. All the applications are currently on the same server, so it's definitely something that could work right now. The only thing I am worried about is for how long we will be able to keep them all on the same server since there's a possibility that a few of the applications will go "global" one day, with a huge amount of users per platform, but that's another problem :-) Thanks for your help! – mwinter Dec 02 '16 at 13:40