Symfony multiple instances of an entity in a form submission - symfony

In Symfony 2.7, I have an entity called Task against which I have persisted 5 entries in the database with the following fields:
ID, TaskName, Date
Now I want to set weights to these entries using a separate entity called Weights (obviously using relationships) the new weights class would have these fields:-
ID, TaskID (Foreign Key), WeightValue
And the rendered form should show all 5 entries of Task type in a twig template and then a text box against each entry to enter weight values like this:-
Task1 [txtbox]
Task2 [txtbox]
....
[SUBMIT]
And when I submit the form all these values should be validated and persisted to the database in Weights table with a related value in TaskID column as a relationship.
What could be the best way to do it?
Thanks

Why exactly does Weight have to be a relationship and not just a new property of task? Please see Doctrine Best Practices.
Just create a FormType for Tag containing both the name and weight and persist everything in the Tag-entity as you would normally. See the cookbook for a good example.

Related

Hibernate JPA returning duplicate records

I am building a spring rest API which will return promo information (promo_code, promo_description, efective_date,expiration_date) and I have the table structure like blow
PROMO_DETAIL
SEQ_NO NUMBER(9), //PK
PROMO_CODE VARCHAR2(9), //PK
PROMO_DESCRIPTION VARCHR2(100),
EFFECTIVE_DATE DATE,
EXPIRATION_DATE DATE
I am using an entity class having promo_code,promo_description,effective_date and expiration_date. I annotated only promo_code with #Id. I am directly returning the entity class as the response.
Problem is when there is 2 records with same promo code(but with different values of other fields). Though In this case JPA returns two records however both records have duplicate values for all fields.
Same type of question was asked here as well "JPA/Hibernate select query returning duplicate records"
Solution is to composite key in entity.
I do not want to add seq_no to entity class because it is not required in response.
I also do not want to create unnecessarily separate response model class. It will create extra overhead of mapping entity to model class.
Is there any other solution to this problem?

How to deal with calculated entity attribute

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.

Multiple table foreign keys for one column.. Is it possible?

I have several entities, that can have multiple relationships.
For example, i have following entitites:
entity_type
tag
tag_assignment
news_post
account
In entity_type table are described all entities that i have in my project (e.g. news posts, blog posts, messages, accounts, everything)
Entity_type table has just id and name fields, name field describes model class name for usability
Tag entity has just id and name. It's standalone entity, that is mapped to other entities later with tag_assignment entity
Tag assignment entity has id, tag_id, entity_type_id and entity_id. Entity_type_id describes in which i can find entity, entity_id specifies entity in the table.
So i want to make following combined foreig key from one column to many tables:
tag_assignment.entity_type_id => entity_type (id)
tag_assignment.entity_id => news_post (id), account (id), etc
Is it possible to make this combined key? I mean if to make dependencies if i delete a row from entity_type table, everything will be dropped/updated in other tables, if i will delete account, only tag_assignments that have foreign key to account table will be deleted.
You should normalize your database by extracting a table called entity. The concept of this table is similar to an abstract class in OOP. The news_post, account and other entities you might have in your database should all reference the entity table. This way you can reference any entity that you have now or might have in the future with a common, specific location.
Also you might want to familiarize yourself with the EAV model. This might help you resolve similar design issues.

Need Help on entity framework

I have 3 tables(Roles,Actions and RoleActionLinks). Roles table has few columns(RoleID,RoleName,Desc). Actions table has few colums(ActionID,ActionName,Desc). In RoleActionLink is created for store the association between Roles and Actions and this table has the columns such as RoleID,ActionID
When I created the data model(edmx). it shows only Role and Action as entity. i did not find RoleActionLink table. but even there is no direct relation between Roles and Actions table, both tables are automatically related using RoleActionLink table.
When i create the new Action, a action record should be populated in Action table(this is works fine). At the same time, i need to populate record in RoleActionLinks table. But i dont have the entity to populate.
Please tell me how to accomplish my needs.
This should work:
newAction.Roles.Add(role1);
newAction.Roles.Add(role2);
Look at navigation properties in your model. There should be EntityCollection called Roles (name may differ).
Entity framework automatically handles n-n tables and creates collections on both sides.

asp.net MVC fetching data with table relations

I have created my Database which has:
Artist, tracks and TracksPerArtists
I can do sql queries through the entity model. For example when I make:
database.TRACK.ToList()
I get the list of track to be shown on index view for example. But my foreign keys come empty. While there is an artist and a track, and the correct row for ArtistsPerTrack, this item in my track.ToList() collection is empty.
Is there a different way to fetch those data?
I came from cakePHP framework in which you can define the Model.recursive property to declare the depth of the relations you want to fetch.
Is there anything similar here?
There is something similar.
If "database" is DataContext and Artist and Tracks have many to many relationship in database, you can fetch related entities using Include clause:
database.TRACK.Include("Artists").ToList()
where "Artists" is the name of property on Track entity.

Resources