0

I am building a social website and I am laying out how the feed will work. I want to use the answer here: How to implement the activity stream in a social network and implement the database design mentioned:

id             
user_id       (int)
activity_type (tinyint)
source_id     (int)  
parent_id     (int)
parent_type   (tinyint)
time          (datetime but a smaller type like int would be better) 

The problem is I don't know how I would map the source_id based off activity_type. If a user registers, I want the source_id to be the user that registered. If someone creates a group the source_id will be the group. I know I can just use simple IDs without keys I just wanted to know if Symfony had some sort of way to do this built in.

If I fetch the feed and the activity_type is user_register I would like to be able to do this to get the source (user) without running an additional query:

$feedEntity->getSource()->getUsername(); //getSource() being the User entity

And if the source_typeis "user_post":

$feedEntity->getSource()->getMessage(); //getSource() being the UserPost entity

I basically just want to find the best way to store this data and make it the fastest.

Community
  • 1
  • 1
Skylord123
  • 482
  • 4
  • 11

1 Answers1

2

Not easy to deal with doctrine and i think it cannot achieved 100% automatically

However, the keyword is table inheritance

http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#single-table-inheritance

I think you could achieve your goal by doing something like this :

  • You create a discriminator map by the type column of the table which tells doctrine to load this entity a UserSource (for example)

  • This UserSource can be an own entity (can be inherited from a base class if you want) where you can decide to map the source_id column to the real User Entity

  • You can use instanceof matching against the namespace of the different entities mapped inside your discriminator map to define different behaviours for the different sources

nixoschu
  • 534
  • 1
  • 3
  • 13
  • what is a discriminator map? – Layton Everson Nov 14 '13 at 01:07
  • Did you read the content of the link ? "The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of a certain type. In the case above a value of “person” identifies a row as being of type Person and “employee” identifies a row as being of type Employee." In short: this map defines which value of your type column will be mapped to which entity – nixoschu Nov 14 '13 at 07:39
  • Yes, I meant to delete that question :/ – Layton Everson Nov 16 '13 at 02:05