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.
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
Related
I have a Symfony 3.4 projet with a REST api. I use JMS serializer.
I have a entity User and I have a route /api/user which return the user id, name , ...
I also have a entity badges which has a relation many to many with user (so a user_badge table). Like I read, when the pivot table have extra column (like in my case on user_badge), I need to create two relation many to one to link my user to badges.
In my route /api/user I add the return on my badges with JMS, I return my badge id and the achievement date (the extra column) from user_badge with the method getUserBadges from my entity User.
But now I want to order by the badges using a column from the badge entity.
How can I achieve this ? The fact than my model user can't access the badges without a heavy foreach. I need to make a request to getting all the badges in the correct order and passing this to JMS.
(I don't know which source file I should provide, cause I don't really know how to achieve it)
Let's say I have a Setting entity with some fields like IntValue, dateValue, stringValue and some linked entities, like countries (ManyToMany to entity Country), languages (ManyToMany to Language) etc.
Settings are created by users and assigned to specific objects (not important here, but I wanted to clarify).
Now I suddenly need to have UserDefaultSetting, which will be the same, but with additional user field (ManyToOne to User entity).
I tried to extend existing Setting entity class with one more field added. The problem is, as I looked at the schema update SQL, it created new table for the new entity, but without all the tables needed to ORM connections (mostly ManyToMany). Just one table with "scalar" fields.
So previously I've had setting table with int_value, date_value etc. but also setting_country and setting_language tables, linking ManyToMany relations. After creating child entity, Doctrine created only user_default_setting table with int_value, date_value etc. and additionally user_id column, but I can't see any relation/link tables.
I know I should've been do it with abstract base entity class, but at the time I started, I didn't know that and now part of the project is on production (don't look at me like that, I blame the client) and I don't want to change that "base" class now. Can I inherit everything from non-abstract entity class in a way it will work?
UPDATE: everything explained. See Cerad's comment. Thanks!
I'm using SF2 and Doctrine2.
I have an Entity Wheel which have a ManyToOne relationship with the Entity Car.
I want to do something like this in a service:
$car_id = 1;
$wheel = new Wheel();
$wheel->setCarId($car_id);
In other terms, I want to be able to link Wheel and Car just by using Car's id, without getting the full object (because I don't need it: I only need only the id and I already have it).
How can I do that?
Get a reference to the car
$carReference = $entityManager->getReference('MyBundle:Car',$carId);
$wheel->setCar($carReference);
Keep in mind that the O in ORM stands for Object. Try to get out of the habit of thinking about database table id's.
Let's say there is an entity called Staff.
It has a number of persistent attributes, such as:
- Name
- Experience
- Age
I want to create a "virtual" attribute, that is based on the Experience and Age, called 'Salary'. For example: $salary = ($experience + $age) * 100
But I don't want to persist the Salary attribute. The reason is that I want to let the Salary attribute get's updated automatically whenever the age or experienced values change.
I have two questions regarding this:
Is the Entity file a good place to store the getSalary() function?
How can I make it so that whenever a Staff entity is called, the salary variable will be filled with the salary that is calculated based on age & experience?
Is the Entity file a good place to store the getSalary() function?
Yes, it is.
Not every field in your entity has to be mapped to a database field.
Also, entities can contain methods other than simple getters and setters. IMO as long as those methods operate on the entity fields, they belong to the entity.
How can I make it so that whenever a Staff entity is called, the salary variable will be filled with the salary that is calculated based on age & experience?
You could use one of the Doctrine's lifecycle events, for example the postLoad event, which is called after entity is loaded to the entity manager.
Note, that you don't have to be storing calculation results in a property. Your calculation is simple and it's probably better to define a getter.
I have 3 tables, one is called Users, one is called Categories and one is a linking table called User_Categories_Map to link users to categories in a many-to-many relationship. The linking table consists of UserId's and CategoryId's. After generating the subsonic classes, I would assume I'd be able to then type User.singleOrDefault(x => x.ID == 1).Categories to select all the categories for a user. However, this doesn't work. If you can understand what I'm trying to accomplish here, can anyone tell me how I can make this work in subsonic? Consequently, I cannot find any documentation on subsonic. Subsonicproject.com only has a short page a few articles about how to set it up. Is there documentation somewhere for subsonic?
int lUserID =1; // suppose 1 is Id of user
CategoriesCollection lCategories = DB.Select().From<Categories>()
.InnerJoin(User_Categories_Map)
.InnerJoin(Users)
.Where(Users.Columns.Id).IsEqualTo(lUserID)
.ExecuteAsCollection<CategoriesCollection>();
It will return collection of categories associated to a specific user..