Kable
Kable

Reputation: 117

Symfony working with different entity managers

I want to create an application with some subsections like a blog and a forum for example. Now I want users to be able to create an account on my site, and with that account they can use the forum and the blog. That's easy so far. But I want to keep the tables in seperate databases to keep a nice and clean structure. Let's say I use 3 databases, one for the UserBundle, one for the BlogBundle and one for the ForumBundle. This requires 3 entity managers. But that means that I can't use relationships from entities from ForumBundle or BlogBundle to the user entity in UserBundle. Simply adding UserBundle under the mappings for the other managers will create new tables in the other databases and that's the thing I'm trying to avoid. So, is there a way to make bundles 'aware' of entities in other bundles? I know it technically isn't a good thing to make bundles dependent on other bundles, but how else would I acheive my idea?

Upvotes: 0

Views: 61

Answers (1)

Cerad
Cerad

Reputation: 48865

One approach that has worked reasonably well for me is to query using the event system.

Assume you are in the Forum Bundle, you have retrieved a list of posts for a given thread and now you need the user information for each post. You make an array of user id's then:

$userIds = array(...list of users you need information for...);

$findUsersEvent = new FindUsersEvent($userIds);

$dispatcher->dispatch('FindUsers',$findUsersEvent);

$users = $findUsersEvent->getUsers();

So now I have a list of user information all nicely indexed by user id from which I can then pull additional information.

The only coupling between the Forum and User bundles is the FindUsersEvent class which could be in a common bundle of some sort. The forum bundles does not care how the users are loaded. The user bundle just needs a listener.

==================================================================

A second approach is to basically use a REST like api for grabbing user information.

Upvotes: 1

Related Questions